我有如下数据
Item# Cust Type
C1 AAA 1
C1 BBB 2
C1 CCC 2
C2 DDD 2
C2 EEE 2
C2 FFF 2
我想基于Item#按行显示,例如
Item# Type1 Type2
C1 AAA BBB
C1 CCC
C2 DDD
C2 EEE
C2 FFF
答案 0 :(得分:2)
您可以使用PIVOT
运算符。但是,对于同一Cust
,您有多个Type
,为了显示所有内容,您需要使用row_number()
select Item#, [1] as Type1, [2] as Type2
from (
select *, rn = row_number() over (partition by Item#, Type order by Cust)
from yourtable
) t
pivot
(
max(Cust)
for Type in ([1], [2])
) p
答案 1 :(得分:0)
选择项目# ,[1] AS Type1 ,[2] AS Type2 FROM(选择* FROM yourtable)tbl PIVOT(最大(成本) FOR [类型]输入([1],[2]) )pvttable