`
chenxueyong
  • 浏览: 336709 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

SQL高级用法(1)

阅读更多

SQL> select * from c
  2  /

        ID HY       SHF           SHL
---------- -------- ------ ----------
         1 部隊     北京           30
         2 建築     北京           50
         3 部隊     湖北          100
         4 建築     湖北          130
         5 建築     上海           60

SQL>
SQL> select nvl(shf,'合計') "****",sum(部隊) 部隊,sum(建築)  建築,sum(sumshl) 合計
  2  from
  3  (
  4  select shf,
  5         max(decode(hy, '建築', shl, 0)) 建築,
  6         max(decode(hy, '部隊', shl, 0)) 部隊,
  7         sumShl
  8    from (select a.id,
  9                 a.hy,
10                 a.shf,
11                 a.shl,
12                 sum(shl) over(partition by shf) sumShl
13            from c a)
14   group by shf, sumShl
15   )
16   group by rollup(shf)
17  /

****         部隊       建築       合計
------ ---------- ---------- ----------
上海            0         60         60
北京           30         50         80
湖北          100        130        230
合計          130        240        370

SQL>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics