Wednesday, 18 September 2013

2 Count in 1 sql

2 Count in 1 sql

ProductTable
ProductID ProductDesc
401 Hotdog
402 Ham
403 Bacon
OrderTable
OrderID OrderPayment NumOrder OrderDate
5001 Cash 3 9-15-2013
5002 Credit 2 9-16-2013
5003 Credit 2 9-17-2013
5004 Cash 3 9-18-2013
OrderDetailsTable
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 -
How I will count the ProductID on how many it was order by cash on each
date and then get the total count of each product?
Sample Output
ProductID ProductDesc CountOnCash OrderDate
401 Hotdog 2 9-15-2013
401 Hotdog 1 9-18-2013
401 Hotdog 3 ---------
402 Ham 1 9-18-2013
402 Ham 1 ---------
403 Bacon 1 9-15-2013
403 Bacon 1 9-18-2013
403 Bacon 2 ---------
Select p.ProductID, p.ProductDesc, count(p.ProductId) as NumOrder,
o.OrderDate
from Product p
inner join OrderDetails od on p.productid = od.productid
inner join Order o on o.orderid = od.orderid
where orderpayment = 'cash'
Group by p.ProductID, p.ProductDesc, o.OrderDate

No comments:

Post a Comment