試圖用COALESCE執行此代碼以將數據組合在一起。執行時,我看到這個錯誤SQL代碼出錯
消息207,級別16,狀態1,行11 無效的列名稱'OrderID'。 消息209,級別16,狀態1,行1 不明確的列名稱'CustomerID'。
SELECT CustomerID,
FirstName,
LastName,
COALESCE(OrderIDCnt,0),
COALESCE(SKUCnt,0),
COALESCE(OrderTotal,0)
FROM Customer as c
left join (SELECT o.CustomerID,
SUM(OrderTotal) AS OrderTotal,
COUNT(OrderedProductSKU) AS SKUCnt,
COUNT(OrderID) AS OrderIDCnt
FROM Orders as o
inner join Orders_ShoppingCart as osc
on osc.OrderNumber=o.OrderNumber
and osc.CustomerID=o.CustomerID
GROUP BY o.CustomerID
)ord
ON ord.CustomerID = c.CustomerID
根據你使用的(MySQL和SQL的服務器,Oracle等) –
SELECT c.CustomerID的SQL請標籤的問題, c.FirstName, c.LastName, COALESCE(c.OrderIDCnt,0), COALESCE(c.SKUCnt,0), COALESCE(c.OrderTotal,0) FROM顧客爲c 左加入(選擇o .CustomerID, SUM(o.OrderTotal)AS OrderTotal, COUNT(o.OrderedProductSKU)AS SKUCnt, COUNT(o.OrderID)AS OrderIDCnt FROM訂單鄰 內部聯接Orders_ShoppingCart作爲osc.OrderNumber OSC = o.OrderNumber 和osc.CustomerID = o.CustomerID GROUP BY o.CustomerID )ORD ON ord.CustomerID = c.CustomerID – Gayathri