February 25, 2005 at 9:57 am
Hi,
I think my aggregate sum function is doing something wrong in the following query
SELECT SUM(Order_details.OrderPrice) AS OrderTotal,
MIN(Products.Manufacturer + ' ' + Products.ProductName) AS ProductNameFull2
FROM Order_details INNER JOIN
Orders ON Order_details.LinkToOrders = Orders.OrderID INNER JOIN
Products ON Order_details.LinkToProducts = Products.SKUCode
GROUP BY Products.Manufacturer + ' ' + Products.ProductName
HAVING (MAX(Orders.OrderDate) > DATEADD(d, - 90, GETDATE())) AND
(Products.Manufacturer + ' ' + Products.ProductName = N'Dell COMPUTER')
When I compare the Ordertotal I get using the above query with the ordertotal I calculate using excel from the following query which does not sum there is a very big difference.
SELECT Products.Manufacturer + ' ' + Products.ProductName AS ProductNameFull2, Orders.OrderID, Orders.OrderDate, Order_details.OrderPrice, Order_details.Quantity,
Orders.Deleted
FROM Order_details INNER JOIN
Orders ON Order_details.LinkToOrders = Orders.OrderID INNER JOIN
Products ON Order_details.LinkToProducts = Products.SKUCode
WHERE (Orders.OrderDate > DATEADD(d, - 90, GETDATE())) AND
(Products.Manufacturer + ' ' + Products.ProductName = N'Dell COMPUTER')
ORDER BY Orders.OrderDate DESC
I cannot understand what am I doing wrong in my first query because I checked and the second query is giving correct results.
Can someone please help to get the sum in a single query. I can use some other query but I just need the total when I group by Products.Manufacturer + ' ' + Products.ProductName.
Thanks.
February 25, 2005 at 12:19 pm
Change
HAVING to a WHERE Clause!
Like
SELECT SUM(Order_details.OrderPrice) AS OrderTotal,
MIN(Products.Manufacturer + ' ' + Products.ProductName) AS ProductNameFull2
FROM Order_details INNER JOIN
Orders ON Order_details.LinkToOrders = Orders.OrderID INNER JOIN
Products ON Order_details.LinkToProducts = Products.SKUCode
WHERE (Orders.OrderDate > DATEADD(d, - 90, GETDATE()) AND
(Products.Manufacturer + ' ' + Products.ProductName = N'Dell COMPUTER')
GROUP BY Products.Manufacturer + ' ' + Products.ProductName
WHERE Filters Before the aggregation HAVING does it afterwards
HTH
* Noel
February 25, 2005 at 12:30 pm
when i try using the above statement it gives me this error
Server: Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'GROUP'.
February 25, 2005 at 12:32 pm
Just a missing parenthesis
SELECT SUM(Order_details.OrderPrice) AS OrderTotal,
MIN(Products.Manufacturer + ' ' + Products.ProductName) AS ProductNameFull2
FROM Order_details INNER JOIN
Orders ON Order_details.LinkToOrders = Orders.OrderID INNER JOIN
Products ON Order_details.LinkToProducts = Products.SKUCode
WHERE (Orders.OrderDate > DATEADD(d, - 90, GETDATE())) AND
(Products.Manufacturer + ' ' + Products.ProductName = N'Dell COMPUTER')
GROUP BY Products.Manufacturer + ' ' + Products.ProductName
HTH
* Noel
February 25, 2005 at 12:46 pm
That works. Thanks a lot.
Also I wanted to know if you could use 2 cases in the case clause. Like i have the following case
(CASE WHEN Max(SalePrice) > 0 THEN Max(SalePrice) ELSE Max(UnitPrice) END)
in this query
SELECT Max(Manufacturer + ' ' + ProductName) As ProductNameFull, ((CASE WHEN Max(SalePrice) > 0 THEN Max(SalePrice) ELSE Max(UnitPrice) END) - Max(cost)) / (CASE WHEN Max(cost) > 0 THEN Max(cost) ELSE 1 END) AS margin INTO #SeqNumberTemp FROM Products Where Products.NotOnWeb = 0 GROUP BY Manufacturer + ' ' + ProductName
I want to change it to
(CASE WHEN Max(SalePrice) > 0 THEN Max(SalePrice) ELSEIF Max(UnitPrice) > 0 Then Max(UnitPrice) Else 1 END)
February 25, 2005 at 1:03 pm
CASE WHEN Max(SalePrice) > 0 THEN Max(SalePrice) ELSE CASE WHEN Max(UnitPrice) > 0 Then Max(UnitPrice) Else 1 END END
February 25, 2005 at 3:13 pm
Cool this also works great. Thanks again for your help. I really appreciate it.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply