0

产品表

ProductID   ProductDesc
401         Hotdog
402         Ham
403         Bacon

订单表

OrderID   OrderPayment    NumOrder
5001      Cash            3
5002      Credit          2
5003      Credit          2
5004      Cash            3

订单明细表

OrderDetailsID   OrderID   ProductID
70001            5001      401         -
70002            5001      401         -
70003            5001      403         -
70004            5002      401
70005            5002      402
70006            5003      402
70007            5003      403
70008            5004      403         -
70009            5004      402         -     
70010            5004      401         -

我将如何计算 ProductID 以现金订购的数量?

样本输出

ProductID   ProductDesc   CountOnCash
401         Hotdog        3
402         Ham           1
403         Bacon         2
4

2 回答 2

1

试试这个代码,

Select p.ProductID , p.ProductDesc, count(p.ProductId)
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order1 o on o.orderid = od.orderid
where orderpayment = 'cash'
Group by p.ProductID, p.ProductDesc

我检查了 sqlfiddle,检查这里http://sqlfiddle.com/#!3/35ec3/5/0

于 2013-09-17T04:04:43.047 回答
0

尝试这个:

select P.ProductID, P.ProductDesc, COUNT(P.ProductId) AS CountOnCach
        from Product AS P
        JOIN OrderDetails AS OD ON P.ProductId = OD.ProductId
        JOIN [Order] AS O ON O.OrderId = OD.OrderId
    WHERE O.OrderPayment = 'Cash'
    GROUP BY P.ProductID, P.ProductDesc
于 2013-09-16T23:14:24.093 回答