日期:2014-05-16  浏览次数:21430 次

oracle 一张两条数据合并为一条数据
         a       b        c           d
1       4027    住宅      1931.32     19
2       4027    非住宅    5300.00     30

需求是这样显示:总套数,总面积,住宅总套数,住宅总面积,非住宅总套数,非住宅总面积
                        49      7231.32 19          1931.32     30            5300.00
条件是a=4027
这个应该怎么写呢?
------解决方案--------------------
select sum(d),sum(c),
       sum(decode(b,'住宅',d,0)),
       sum(decode(b,'住宅',c,0)),
       sum(decode(b,'非住宅',d,0)),
       sum(decode(b,'非住宅',c,0))  from table where a=4027 group by a
------解决方案--------------------

SELECT SUM(D),
       SUM(C),
       SUM(DECODE(B, '住宅', D, 0)),
       SUM(DECODE(B, '住宅', C, 0)),
       SUM(DECODE(B, '非住宅', D, 0)),
       SUM(DECODE(B, '非住宅', D, 0))
  FROM TABLE
 WHERE A = 4027

------解决方案--------------------
引用:

SELECT SUM(D),
       SUM(C),
       SUM(DECODE(B, '住宅', D, 0)),
       SUM(DECODE(B, '住宅', C, 0)),
       SUM(DECODE(B, '非住宅', D, 0)),
       SUM(DECODE(B, '非住宅', C, 0))
  FROM TABLE
 WHERE A = 4027
 改了下