My table MasterSaleslooks like this:
SalesDate | Category | Total
1/1/2000 01 100
1/1/2000 02 110
1/2/2000 01 80
1/2/2000 03 20
and the table Categorylooks like
ID | Name
----------
01 | A
02 | B
03 | C
04 | D
my request looks like this:
SELECT m.SalesDate, c.Name, SUM(ISNULL(m.Total,0)) AS TotalSales
FROM MasterSales m
LEFT JOIN Category c ON c.ID = m.Category
WHERE m.SalesDate BETWEEN '1/1/2000' AND '1/2/2000'
The result I want is as follows:
SalesDate | Name | TotalSales
1/1/2000 A 100
1/1/2000 B 110
1/1/2000 C 0
1/1/2000 D 0
1/2/2000 A 80
1/2/2000 B 0
1/2/2000 C 20
1/2/2000 D 0
but the result that I get is as follows:
SalesDate | Name | TotalSales
1/1/2000 A 100
1/1/2000 B 110
1/2/2000 A 80
1/2/2000 C 20
I already tried using RIGHT JOINinstead LEFT JOINand switching the table to FROM, but the result is still the same. can someone help explain to me why it will not work properly?
PS: I am using SQL Server 2005 (if that matters)
source
share