views:

25

answers:

5

I have sales table data as shown below

Sales Table Data

I want it to display group wise sales according to Date. Sales table contains data for different groups but my query shows only two rows.

The SQL Query:

select 
    i.gName, 
    sum(Quantity) as '180ml', 
    isnull((select sum(Quantity)
        from saleslog 
        where BillDate='12-10-2010' 
        and pSize=375 and pGroup=i.gCode),0) as '375ml', 
    isnull((select sum(Quantity)
        from saleslog 
        where BillDate='12-10-2010' 
        and pSize=500 and pGroup=i.gCode),0) as '500ml', 
    isnull((select sum(Quantity)
        from saleslog 
        where BillDate='12-10-2010' 
        and pSize=750 and pGroup=i.gCode),0) as '750ml', 
    isnull((select sum(Quantity)
        from saleslog 
        where BillDate='12-10-2010' 
        and pSize=1000 and pGroup=i.gCode),0) as '1000ml', 
    isnull((select sum(Quantity)
        from saleslog 
        where BillDate='12-10-2010' 
        and pSize=2000 and pGroup=i.gCode),0) as '2000ml' 
from saleslog as s
    inner join ItemGroup as i on s.pGroup=i.gCode 
where BillDate='12-10-2010' 
    and i.gCode=pGroup 
    and pSize=180 
group by i.gCode,i.gName

Output of above query

WHISKY 5 2 0 0 0 0
RUM     82 0 0 45 0 0

It is showing these results, but I expected it to list all product groups as follows:

Product Group Table :

1 BRANDY         1
2 WHISKY         2
3 RUM         3
4 GIN         4
5 VODKA         5
6 BEER         8
7 WINE         6
8 LIQUOR         7
9 SCOTCH WHY 9
10 LUBRICANT 15
11 UNTAXABLE 16
12 O/S LIQUOR 10
13 RTD         11
14 275 ML         12

What's wrong with my query?

A: 

Try changing your FROM clause onwards to:

from ItemGroup as i
    LEFT OUTER JOIN saleslog as s ON i.gCode = s.pGroup AND s.BillDate = '12-10-2010' AND s.pSize=180
group by i.gCode,i.gName
AdaTheDev
+1  A: 

Ok. I think you need to move the 180mL query into a subquery with the others. Something like this:

select i.gName,
isnull((select sum(Quantity)from saleslog where BillDate='12-10-2010' and pSize=180 and pGroup=i.gCode),0) as '180ml', 
isnull((select sum(Quantity)from saleslog where BillDate='12-10-2010' and pSize=375 and pGroup=i.gCode),0) as '375ml', 
isnull((select sum(Quantity)from saleslog where BillDate='12-10-2010' and pSize=500 and pGroup=i.gCode),0) as '500ml',
isnull((select sum(Quantity)from saleslog where BillDate='12-10-2010' and pSize=750 and pGroup=i.gCode),0) as '750ml',
isnull((select sum(Quantity)from saleslog where BillDate='12-10-2010' and pSize=1000 and pGroup=i.gCode),0) as '1000ml',
isnull((select sum(Quantity)from saleslog where BillDate='12-10-2010' and pSize=2000 and pGroup=i.gCode),0) as '2000ml' 
from saleslog as s 
inner join ItemGroup as i on s.pGroup=i.gCode 
where BillDate='12-10-2010' 
group by i.gCode, i.gName
Blorgbeard
This is correct. (Cant upvote atm)
Graphain
OK its showing all group, thanks..programming, 1 code error or misplace can do havoc. thanks again for prompt reply....
RAJ K
But if there's not SalesLog record for an item on 12-10-2010, then no record will be returned for that group still.
AdaTheDev
+1  A: 

What datatype is this BillDate??

If it is DATETIME, then this statement here

BillDate = '12-10-2010'

will only select those purchases made on 12-10-2010 at midnight (0:00:00 hours).

You need to be more careful with your date queries! DATETIME always also contains a time portion - so if you want all purchases on the 12-10-2010, you need to use:

WHERE BillDate BETWEEN '12-10-2010 00:00:00' AND  '12-10-2010 23:59:59'

or alternatively:

WHERE DAY(BillDate) = 12 AND MONTH(BillDate) = 10 AND YEAR(BillDate) = 2010
marc_s
This is also important (can't upvote atm).
Graphain
Good point - though I'd use: BillDate >= '2010-10-12' AND BillDate < '2010-10-13'as I've seen records missed before from the last second of the day with the BETWEEN approach (i.e. any records 23:59:59.001 to 23:59:59.999(
AdaTheDev
A: 

That query makes no sense semantically but works once you think hard about it.

For instance:

You are selecting the SUM of all sales for the group as your 180ml:

sum(Quantity) as '180ml'

But you are conditioning by the size of 180ml on the total to counter that

where pSize=180

I would say it is because the other groups don't have 180ml sizes for sale.

Graphain
A: 

You query only returns product groups which have at least 1 180ml sale. Change the INNER JOIN to a subselect, as you did with the other product sizes, and you will get all product groups in the result set.

devio