I'm not very good with SQL queries but I attempted to write this one:
SELECT DATEPART(YY,Orders.OrderDate) as Year,
DATEPART(MM,Orders.OrderDate) as Month,
(SUM(case when OrderDetails.ProductCode = 'XXX' then
OrderDetails.ProductPrice else 0 end) + SUM(Orders.Total))
AS XXX
FROM Orders
LEFT JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID
WHERE Orders.OrderStatus = 'Shipped'
GROUP BY DATEPART(MM,Orders.OrderDate), DATEPART(YY,Orders.OrderDate)
ORDER BY DATEPART(YY,Orders.OrderDate),DATEPART(MM,Orders.OrderDate)
The OrderDetails
is linked to the Orders
table by the field OrderID
. In this SELECT
query I'm trying to get the SUM of OrderDetails.ProductPrice
when the OrderDetails.ProductCode
is XXX
and add it to the Orders.Total
to get total amounts for each month/year.
The query is working except for one problem (that's probably either a amateur mistake or has been worked around several times). When performing the LEFT JOIN
, the OrderDetails
table can have multiple records linked to the Orders
table which is throwing bad results in the SUM(Orders.Total)
. I've isolated that issue I just can't seem to fix it.
Can anybody point me in the right direction?