t-sql查询未显示预期结果

时间:2010-06-30 07:42:54

标签: sql-server-2005 tsql

我的销售表数据如下所示

Sales Table Data http://lh5.ggpht.com/_KT7tmVVBHFM/TCryAax1JlI/AAAAAAAAAFk/zahMq4RoOuw/s144/Sales.png

我希望它根据日期显示分组销售情况。 Sales表包含不同组的数据,但我的查询只显示两行。

SQL查询:

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

以上查询的输出

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

它显示了这些结果,但我希望它列出所有产品组,如下所示:

产品组表:

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

我的查询有什么问题?

5 个答案:

答案 0 :(得分:1)

确定。我认为您需要将180mL查询与其他查询一起移动到子查询中。像这样:

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

答案 1 :(得分:1)

这个BillDate是什么数据类型?

如果是DATETIME,那么此声明

BillDate = '12-10-2010'

仅会选择在12月10日午夜(0:00:00)进行的购买。

您需要更加小心日期查询! DATETIME 始终还包含时间部分 - 因此,如果您想要在12-10-2010进行所有购买,则需要使用:

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

或者:

WHERE DAY(BillDate) = 12 AND MONTH(BillDate) = 10 AND YEAR(BillDate) = 2010

答案 2 :(得分:0)

尝试将FROM子句改为:

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

答案 3 :(得分:0)

这个查询在语义上没有任何意义,但是一旦你认真考虑它就会起作用。

例如:

您正在为该组选择所有销售的总和为180ml:

sum(Quantity) as '180ml'

但你要调整总量为180毫升以对抗

where pSize=180

我想说这是因为其他团体没有180ml尺寸可供出售。

答案 4 :(得分:0)

您只查询返回至少有1 180ml促销的产品组。将INNER JOIN更改为子选择,就像使用其他产品尺寸一样,您将获得结果集中的所有产品组。