3
我有以下功能: -是否可以在SQL PIVOT中使用動態創建的值?
ALTER FUNCTION fncTest()
RETURNS TABLE
AS
RETURN
(SELECT *
FROM (SELECT TOP 100 PERCENT sOrderType,
SUM(iQty) AS iOrdQty,
(YEAR(dReqd) * 100) + DATEPART(Week, dReqd) AS iWkNo
FROM tblOrderBook
GROUP BY sOrderType,
dOrdered) AS tblTemp PIVOT(SUM(Qty) FOR iWkNo IN
([201118], [201119], [201120], [201121], [201122])) AS pvtTemp)
這給了我一個樞軸表顯示了周訂單qtys 2011年
18-22是否有可能與動態更換硬編碼周基於GETDATE()的日期。
ie:
- 取代
[201118]
與(YEAR(GETDATE()+7)*100)+DATEPART(week,GETDATE()+7)
- 與
(YEAR(GETDATE()+14)*100)+DATEPART(week,GETDATE()+14)
- 取代
[200119]
與(YEAR(GETDATE()+21)*100)+DATEPART(week,GETDATE()+21)
等替代[200120]
...
感謝。
同類問題:http://stackoverflow.com/questions/2363286/is-it-possible-that-we-pass-a-query-to-pivot-as-column-list-in-sql-server – JeffO 2011-05-18 14:56:39