Friday, April 11, 2008

What is wrong with this query?

SELECT
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1,
C.City, C.State, SUM(S.Sales) as TotalSales
FROM
Customers C
INNER JOIN Sales S
ON C.CustomerID = S.CustomerID
GROUP BY
C.CustomerID, C.CustomerName,
C.CustomerType, C.Address1, C.City, C.State

3 comments:

Tahir said...

I believe, the problem lies with the function SUM and the Inner Join. An Inner join cannot be placed on a fied which supposed to be used as a cummulative one!

Hope to get more ideas on this ...

Aamir said...

Problem is in Group by clause it must contains SUM(S.Sales).

(Aamir Ali Shah)
(syedsoftware@hotmail.com)

M. Adil Amin said...

Problem is that INNER JOIN .I think we need to put LEFT JOIN insteadof INNER JOIN for that becuase if is there no Sales against customer that customer will never show in the Query results.