sql server :
如果品名有限可知:
select 地点,
Nullif(sum(case when 品名='A' then 销量 else 0 end),0) as A,
Nullif(sum(case when 品名='B' then 销量 else 0 end),0) as B,
Nullif(sum(case when 品名='C' then 销量 else 0 end),0) as C,
Nullif(sum(case when 品名='D' then 销量 else 0 end),0) as D
from tablename
grou by 地点
如果品名有限可知:
select 地点,
Nullif(sum(case when 品名='A' then 销量 else 0 end),0) as A,
Nullif(sum(case when 品名='B' then 销量 else 0 end),0) as B,
Nullif(sum(case when 品名='C' then 销量 else 0 end),0) as C,
Nullif(sum(case when 品名='D' then 销量 else 0 end),0) as D
from tablename
grou by 地点
没学过,谢谢!
请问这是标准的SQL语句么,我要在SYBASE中使用
sum(case when 品名='A' then 销量 else 0 end) as A,
sum(case when 品名='B' then 销量 else 0 end) as B,
sum(case when 品名='C' then 销量 else 0 end) as C,
sum(case when 品名='D' then 销量 else 0 end) as D
from tablename
grou by 地点不过结果有点不一样: 品名
地点 | A B C D
------------------------------------——
甲 | 100 200 300 0
乙 | 0 50 0 80
丙 | 200 400 0 0
(select sum(销量) from tablename where 品名='A') as A,
(select sum(销量) from tablename where 品名='B') as B,
(select sum(销量) from tablename where 品名='C') as C,
(select sum(销量) from tablename where 品名='D') as D
from tablename
原来SYBASE可以按case的方法做,我开始试的时候写错了,深表感谢!顺便说一下,最后的一个方法没用,结果每一行全是一样的。