Oracle 交叉表 固定值
如果速度固定只有0 20 50 100 150这几个值,可以:
select
No,
sum(decode(速度,'0',值,0)),
sum(decode(速度,'20',值,0)),
sum(decode(速度,'50',值,0)),
sum(decode(速度,'100',值,0)),
sum(decode(速度,'150',值,0))
from 表
group by No
Top
7 楼tonyring()回复于 2006-11-28 22:51:47 得分 10
select No,
sum(case when 速度 = 0 then 值 else 0 end) 0,
sum(case when 速度 = 20 then 值 else 0 end) 20,
sum(case when 速度 = 50 then 值 else 0 end) 50,
sum(case when 速度 = 100 then 值 else 0 end) 100,
sum(case when 速度 = 150 then 值 else 0 end) 150,
sum(case when 速度 = 200 then 值 else 0 end) 200
from 表
group by No
-- group by No 无合计
-- group by cube(No) 出现顶行合计
-- group by rollup(No) 出现底行合计
order by No
不用动态sql,那就需要知道速度值都有多少,然后decode或case了。
如果速度固定只有0 20 50 100 150这几个值,可以:
select
No,
sum(decode(速度,'0',值,0)),
sum(decode(速度,'20',值,0)),
sum(decode(速度,'50',值,0)),
sum(decode(速度,'100',值,0)),
sum(decode(速度,'150',值,0))
from 表
group by No
Top
7 楼tonyring()回复于 2006-11-28 22:51:47 得分 10
select No,
sum(case when 速度 = 0 then 值 else 0 end) 0,
sum(case when 速度 = 20 then 值 else 0 end) 20,
sum(case when 速度 = 50 then 值 else 0 end) 50,
sum(case when 速度 = 100 then 值 else 0 end) 100,
sum(case when 速度 = 150 then 值 else 0 end) 150,
sum(case when 速度 = 200 then 值 else 0 end) 200
from 表
group by No
-- group by No 无合计
-- group by cube(No) 出现顶行合计
-- group by rollup(No) 出现底行合计
order by No
不用动态sql,那就需要知道速度值都有多少,然后decode或case了。