日期:2014-05-17  浏览次数:20751 次

oracle关联统计查询
Typeid typename
1 工业
2 商业
3 农业
...

ID Typeid Time Value
1 1 10:00 123
1 2 10:00 321
1 3 10:00 213


想要的结果:
Time 工业 农业 商业 ...
10:00 123 213 321 ...
...
...
...

数据量应该是蛮大的
至少是50万条吧
5分钟执行一次查询,兼顾性能!
求大神指导
oracle关联统计查询

------解决方案--------------------
select m.time, sum(decode(m.typename,'工业',m.value,null)) '工业',
sum(decode(m.typename,'农业',m.value,null)) '农业',
sum(decode(m.typename,'商业',m.value,null)) '商业'
from
(SELECT b.ID, a.typename, b.TIME, b.VALUE
  FROM a, b
 WHERE a.typeid = b.typeid) m
 group by m.time
 order by m.time
------解决方案--------------------
with t as 
(select b.time,a.typename,sum(b.value) value from b left join a 
on b.typeid=a.typeid
group by b.time,a.typename)
select * from t pivot (sum(value) for typename in ('工业','农业','商业'));