有人可以帮助您使用以下PIVOT表吗?
我希望每个产品都有一行,当前的交易价格,然后是5列,并列出历史交易价格。
e.g
ProductID Trade T1 T2 T3 T4 T5
12345 100 99.44 96.44 102.45 101.12 99.99
等
当前代码,在每个透视列中返回NULL
SELECT ProductID,ProductTradePrice AS Trade,
[0], [1], [2], [3], [4]
FROM
(
SELECT ChangeLogID, p.ProductID , ProductName, ProductTradePrice,
CONVERT(decimal(18,2),ChangeLogOldValue) AS ChangeLogOldValue
FROM tblProduct p
INNER JOIN tblChangeLog cl ON p.ProductID = cl.ProductID
WHERE PrimarySupplierNumber = 7
AND ChangeLogItemChanged = 'ProductTradePrice'
) S
PIVOT
(
AVG(ChangeLogOldValue)
FOR ChangeLogID IN ([0], [1], [2], [3], [4])
) AS PivotTable;