我有一个包含以下内容的表:
EXTDEP EXTCODE PRICE
200 10001 200
500 10001 25
600 10001 36
我想要的结果:
EXTCODE PRICE200 PRICE500 PRICE600
10001 200 25 36
我得到的结果是:
EXTCODE PRICE200 PRICE500 PRICE600
10001 NULL NULL 36
10001 NULL 25 NULL
10001 200 NULL NULL
我的代码:
SELECT
[ExtCode]
case when extdep = '200' then price1 end as '200',
case when extdep = '500' then price1 end AS '500',
case when extdep = '600' then price1 end AS '600'
有任何想法吗? :)
最佳答案
尝试执行以下操作,将聚合应用于CASE语句:
SELECT
[ExtCode],
MAX(case when extdep = '200' then price1 end) as '200',
MAX(case when extdep = '500' then price1 end) AS '500',
MAX(case when extdep = '600' then price1 end) AS '600'
FROM your_table
GROUP BY ExtCode