0
我試圖使下面的Pivot SQL查詢動態。SQL:服務器動態PIVOT查詢
應該從下面的查詢中檢索動態客戶的列表。
客戶列表查詢
SELECT
Cust_Name
FROM dbo.Customer
INNER JOIN dbo.Service
ON dbo.Customer.Cust_ID = dbo.Service.Cust_ID
WHERE [Next_Service] BETWEEN '2017-09-01' AND '2015-09-10'
樞軸查詢
Select *
from
(
select Customer.Cust_Name,Agreementlist.ProductQty,Store.Product_Name as Refill
from dbo.Agreement INNER JOIN dbo.Agreementlist ON dbo.Agreement.Agreement_ID = dbo.Agreementlist.Agreement_ID INNER JOIN dbo.Customer
ON dbo.Customer.Cust_ID = dbo.Agreement.Cust_ID INNER JOIN Store on Store.Pro_ID = dbo.Agreementlist.ProID where CatID='2' and Agreement.Status='Approved'
) x
pivot
(
sum(ProductQty)
for Cust_Name in ("list of customers resulted from the first query")
) p
錯誤:消息207,級別16,狀態1,行14 無效的列名稱GRUPPE「。 – Ayman
抱歉刪除gruppe - 它來自我的舊SQL - 我只是編輯了行。 – plaidDK
剛剛編輯我的SQL,所以如果您的客戶名稱中有空格,請設置括號。然後我將SQL從SQL中移除到循環中。 – plaidDK