0

3 つの異なるテーブルから結果を返す SQL クエリを作成するのに助けが必要です。2 つのテーブルのステートメントを正常に作成しましたが、3 つすべてを取得できません。

テーブルと関連フィールドは次のとおりです。

Order        OrderItem       Product
-------      -----------     -----------
orderId      orderId         productCode
status       productCode     productName
             quantity

目的の結果セットを探しています:

[productCode]    [productName]    [orderCount]
-------------    -----------      ----------
123              widget           3
456              thing            2

結果は、 from 、 from 、および製品が注文および出荷された回数によってグループ化productCodeOrderItemproductNameますProduct

出荷ステータスは のstatusフィールドから取得されOrderます。つまり、フィールドが「出荷済み」OrderItemである行のみを含めます。statusOrder

どんな助けでも大歓迎です。

4

2 に答える 2

0

これを試して:

select 
    p.productCode, p.productName, count(o.OrderId) as orderCount,
    sum(oi.quantity) as orderQuantity -- Optional (if you want the sum of products ordered)
from
    [product] as p
    inner join [orderItem] as oi on p.productCode = oi.productcode
    inner join [order] as o on oi.orderId = o.orderId
group by 
    p.productCode, p.productName
于 2013-04-11T16:47:17.297 に答える