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关联统计查询

解决方案 »

  1.   

    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
      

  2.   

    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 ('工业','农业','商业'));