请参阅下表:
对于每个CustomerID + PaymentID + ClaimNum,ItemID& 3有3个NULL值。数量。
我想取这3个NULL值并将它们作为COLUMNS。
DeductionID 1085 =服务费(列名)
DeductionID 486 =运费(列名)
DeductionID 559 =手续费(栏名)
例如,让我们将ClaimNum用于突出显示的NULL:
做我想做的事,最终会有以下几列:
CustomerID,PaymentID,ClaimNum,ItemID,数量,金额,服务费,运费,手续费
我不确定我是否需要在这里进行CROSS JOIN,PIVOT或UNPIVOT。我以前从来没有这样做过,阅读微软的网站并没有真正起到帮助作用。任何人都可以帮我一把吗?
如果您需要更多信息,请与我们联系。我感谢所有的帮助!!
答案 0 :(得分:1)
您可以转动,然后联接回原始数据。
即:
select
yourtable.* , Service, handling, freight
from yourtable
inner join
(
select CustomerID, PaymentID, ClaimNum,
[1085] as Service, [486] as freight, [559] as handling
from
(select customerid, paymentid, claimnum, DeductionID, Amount from yourtable) t
pivot
(sum (amount) for DeductionID in ([1085],[486],[559]))p
) pt
on yourtable.CustomerID = pt.CustomerID
and yourtable.PaymentID = pt.PaymentID
and yourtable.ClaimNum = pt.ClaimNum
where ItemID is not null