2013-12-13 61 views
0

我是數據庫領域的新手,我剛開始學習並發現SQLZoo網站非常有幫助。 我在AdventureWorks數據庫中遇到了這個問題。SQLZoo難題#12 AdventureWorks Assessments

http://sqlzoo.net/wiki/AdventureWorks_hard_questions

12.For每個訂單顯示的SalesOrderID和小計計算三種方式: A)從的SalesOrderHeader B)OrderQty *單價 的C和)OrderQty總和* ListPrice

對於某種原因,我無法弄清楚這一點。任何幫助是極大的讚賞。

這是我的查詢:

SELECT 
    A.SalesOrderID, 
    A.SubTotal AS SubTotalA, 
    B.SubtotalB, 
    C.SubtotalC 
FROM SalesOrderHeader A 
JOIN 
(
    SELECT SUM(sodB.OrderQty*sodB.UnitPrice) AS SubTotalB 
    FROM SalesOrderDetail sodB 
    JOIN ProductAW pawB 
    ON pawB.ProductID=sodB.ProductID 
    GROUP BY sodB.SalesOrderID 
) AS B 
JOIN 
(
    SELECT SUM(sodC.OrderQty*pawC.ListPrice) AS SubTotalC 
    FROM SalesOrderDetail sodC 
    JOIN ProductAW pawC 
    ON pawC.ProductID=sodC.ProductID 
    GROUP BY sodC.SalesOrderID 
) AS C 

出於某種原因,當我運行任何SELECT語句B或C單獨外JOIN我得到我所期待的結果,但是當我把那裏面JOIN爲SubTotalB所有值或小計C重複顯示與第一行值相同的結果。

SELECT SUM(sodC.OrderQty*pawC.ListPrice) AS SubTotalC 
    FROM SalesOrderDetail sodC 
    JOIN ProductAW pawC 
    ON pawC.ProductID=sodC.ProductID 
    GROUP BY sodC.SalesOrderID 

OR

SELECT SUM(sodC.OrderQty*pawC.ListPrice) AS SubTotalC 
    FROM SalesOrderDetail sodC 
    JOIN ProductAW pawC 
    ON pawC.ProductID=sodC.ProductID 
    GROUP BY sodC.SalesOrderID 

回答

0

你的外部查詢沒有任何ON子句。這將導致表B中的每個記錄針對表A中的每條記錄進行設置。因此,如果表A有5條記錄,並且表B有5條記錄,那麼總共有25條記錄。那麼當然,所有這些記錄也會與表C相乘。所有的AB記錄將針對表C中的每條記錄設置。

我爲每個子查詢SELECT語句添加了一個ProductID,然後將ON語句添加到外部查詢中的每個JOINS以加入匹配產品ID。我希望這是你的目標。

SELECT 
    A.SalesOrderID, 
    A.SubTotal AS SubTotalA, 
    B.SubtotalB, 
    C.SubtotalC 
FROM SalesOrderHeader A 
JOIN 
(
    SELECT SUM(sodB.OrderQty*sodB.UnitPrice) AS SubTotalB, 
    pawB.ProductID 
    FROM SalesOrderDetail sodB 
    JOIN ProductAW pawB 
    ON pawB.ProductID=sodB.ProductID 
    GROUP BY sodB.SalesOrderID 
) AS B 
ON A.ProductID = B.ProductID 
JOIN 
(
    SELECT SUM(sodC.OrderQty*pawC.ListPrice) AS SubTotalC, 
    pawC.ProductID 
    FROM SalesOrderDetail sodC 
    JOIN ProductAW pawC 
    ON pawC.ProductID=sodC.ProductID 
    GROUP BY sodC.SalesOrderID 
) AS C 
ON A.ProductID = C.ProductID 
+1

在您指出外部查詢缺少ON子句之後,我也意識到B和C查詢缺少對其SalesOrderID的引用。我在您的解決方案中添加了sob.SalesOrderID&sodC.SalesOrderID,現在它可以工作!非常感謝 – dePaula

1

這是最終的解決方案:

SELECT 
    A.SalesOrderID, 
    A.SubTotal AS SubTotalA, 
    B.SubtotalB, 
    C.SubtotalC 
FROM SalesOrderHeader A 
JOIN 
(
    SELECT SUM(sodB.OrderQty*sodB.UnitPrice) AS SubTotalB, sodB.SalesOrderID 
    FROM SalesOrderDetail sodB 
    JOIN ProductAW pawB 
    ON pawB.ProductID=sodB.ProductID 
    GROUP BY sodB.SalesOrderID 
) AS B 
ON A.SalesOrderID = B.SalesOrderID 
JOIN 
(
    SELECT SUM(sodC.OrderQty*pawC.ListPrice) AS SubTotalC, sodC.SalesOrderID 
    FROM SalesOrderDetail sodC 
    JOIN ProductAW pawC 
    ON pawC.ProductID=sodC.ProductID 
    GROUP BY sodC.SalesOrderID 
) AS C 
ON A.SalesOrderID = C.SalesOrderID 
0

我喜歡這個網站,但我希望能有一個比較答案,因爲我只是想知道,如果我的查詢以最好的方式完成的地方。這是我想出了同樣的問題:

SELECT soh.salesorderid, soh.subtotal AS "SubTotal A", SUM(sod.orderqty * sod.unitprice) AS "SubTotal B", SUM(sod.orderqty * paw.ListPrice) AS "SubTotal C" FROM SalesOrderHeader soh JOIN SalesOrderDetail sod (ON sod.salesorderid = soh.salesorderid) JOIN ProductAW paw (ON paw.productid = sod.productid) GROUP BY soh.salesorderid

我還在學習過,所以如果有人想插話,讓我知道我怎麼能改善它,請不要。

-1
select t1.SalesOrderID ID,t1.SubTotal SUBTOTAL, 
sum(t2.OrderQty*t2.UnitPrice) SUBTOTAL 
,sum(t2.OrderQty*t3.ListPrice) SUBTOTAL 
from SalesOrderHeader t1 join SalesOrderDetail t2 on t1.SalesOrderID=t2.SalesOrderID join ProductAW t3 on t3.productid=t2.productid 
group by t1.SalesOrderID,t1.Subtotal 
+1

請編輯您的答案。將代碼添加到代碼標籤中以提高可讀性,並添加說明您做了什麼以及爲什麼以及如何修復OP的問題。 – Bobby