我有一张表如下
Ident type id value
A CP 1 1000
A CP 2 EXIST
A GVI 1 100
A GVI 2 NOT EXIST
我需要按如下方式查看它们
Ident type value( id=1) value(ld=2)
A CP 1000 Exist
A GVI 100 NOT EXIST
知道该怎么做吗?
答案 0 :(得分:2)
使用Conditional Aggregate
select Ident,
type,
max(case when id=1 then value end) as [value(ld=1)],
max(case when id=2 then value end) as [value(ld=2)]
from yourtable
group by Ident,type
或您也可以使用Pivot
运算符
SELECT ident,
type,
[1] AS [value(ld=1)],
[2] AS [value(ld=2)]
FROM (SELECT *
FROM yourtable) A
PIVOT (Max(value)
FOR id IN ([1],
[2])) pv