【发布时间】:2016-03-04 23:39:22
【问题描述】:
我遇到了一个问题,我看到其他人也有类似的问题,但这些问题的答案似乎不适用于我的情况。这是我的第一个问题,如有任何格式问题,请提前原谅我,并感谢您提供的任何见解。
我的#TempTBData 看起来像这样:
InvoiceProductID ContactID ContactName ChargeDescription Amount
191 1832 Gloria Cheung Cruise Fare 500.00
191 1886 John Novosad Cruise Fare 500.00
191 2011 Christopher Yong Cruise Fare 100.00
我的枢轴代码如下所示:
SELECT DISTINCT<br>
[InvoiceProductID]<br>
,[ChargeDescription]<br>
,[Christopher Yong],[Gloria Cheung],[John Novosad]<br>
FROM #TempTBData<br>
PIVOT(MAX([Amount])<br>
FOR [ContactName] IN ([Christopher Yong],[Gloria Cheung],[John Novosad])) AS PVTTable
..我的 PIVOT 结果如下所示:
InvoiceProductID ChargeDescription Christopher Yong Gloria Cheung John Novosad
191 Cruise Fare NULL NULL 500.00
191 Cruise Fare NULL 500.00 NULL
191 Cruise Fare 100.00 NULL NULL
..我希望结果是:
InvoiceProductID ChargeDescription Christopher Yong Gloria Cheung John Novosad
191 Cruise Fare 100.00 500.00 500.00
请让我知道我做错了什么。
【问题讨论】:
-
感谢@Ajmot 的格式化帮助!我无法在有限的所见即所得编辑器中看到如何以这种方式格式化。
标签: sql-server tsql stored-procedures pivot