私は以下のような基本構造を持っています
Sales.Customers Sales.Orders Sales.OrderDetails
--------------- ------------ ------------------
country orderid orderid
custid custid qty
したがって、米国の顧客を返す必要があり、顧客ごとに合計注文数と合計数量を返します。私はそのようなクエリを書きました:
SELECT
C.custid, SUM(O.orderid) as numorders,
SUM(OD.qty) as totalqty
FROM Sales.Customers AS C
JOIN Sales.Orders AS O
ON C.custid = O.custid
JOIN Sales.OrderDetails AS OD
ON O.orderid = OD.orderid
WHERE country = 'USA'
GROUP BY C.custid;
残念ながら、私はそのような結果を得ます:
custid numorders totalqty
----------- ----------- -----------
32 235946 345
36 94228 122
43 21027 20
....... ..... ....
それ以外の
custid numorders totalqty
----------- ----------- -----------
32 11 345
36 5 122
どこが間違っているのか理解できません。