日期:2014-05-17 浏览次数:20742 次
with t1 as
(
select 1 rid,date'2013-09-10' rdate,2 tid,50 money from dual union all
select 1 rid,date'2013-09-10' rdate,4 tid,40 money from dual union all
select 2 rid,date'2013-09-11' rdate,1 tid,100 money from dual
),t2 as
(
select 1 rid,date'2013-09-10' rdate,1 tid,100 money from dual union all
select 2 rid,date'2013-09-11' rdate,2 tid,150 money from dual union all
select 1 rid,date'2013-09-11' rdate,3 tid,120 money from dual union all
select 2 rid,date'2013-09-11' rdate,1 tid,110 money from dual
)
select rid,rdate,
sum(decode(tid,1,money,0)) "类别1",sum(decode(tid,2,money,0)) "类别2",
sum(decode(tid,3,money,0)) "类别3",sum(decode(tid,4,money,0)) "类别4"
from (select * from t1 union all select * from t2) t
group by rid,rdate
order by rid,rdate
rid rdate 类别1 类别2 类别3 类别4
------------------------------
1 1 2013/9/10 100 50 0 40
2 1 2013/9/11 0 0 120 0
3 2 2013/9/11 210 150 0 0