具体问题如下: 
/* 
数据表表1:salePrice销售商品价格明细: 
门店号shopid 小票号saleno  价格price  日期begindate 
    A01          2008122601      11.5  2008-12-26 12:33:55:789 
    A01          2008122601      99    2008-12-26 12:33:55:789 
    A01          2008122702      66    2008-12-27 3:33:55:789 
    A02          2008122703      55    2008-12-27 3:33:55:789 
    A02          2008122703      33    2008-12-27 3:33:55:789 
    A02          2008122703      22.5  2008-12-27 3:33:55:789 
    A03          2008122904      11    2008-12-29 8:38:55:789 
表的每一行代表所销售的一条商品记录,一张小票可能拥有一条或几条记录,同一张小票的小票号、门店号与日期相同 数据表表2:salegoods销售商品数量与性质明细: 
门店号shopid 小票号saleno     商品名称    数量 
    A01          2008122601  023手帕纸    3 
    A01         2008122601  10元购物券    1      
    A01         2008122702  88饼干      2.3    
    A02         2008122703  51好运鞋      7    
    A02         2008122703  10元购物券    2    
    A02         2008122703  99洒         1 
   A02        2008122703   10元购物券    1  
    A03         2008122904   21熊猫烟      3    
表的每一行代表所销售的一条商品记录,一张小票可能拥有一条或几条记录,同一张小票的小票号、门店号相同 
小票号的前8位表示小票产生时间即交易时间
销售商品时,将商品的物理信息与件数记入数据表2:salegoods表 的同时将该商品的价格信息记入数据表1 现在20081226-20090115期间进行一种促销活动: 
单张小票满50元赠一张10元购物券,满100赠两张,满150赠3张,以此类推 
送出的10元购物券需记入数据表2:salegoods表 问题求助: 
期待结果 
门店号shopid 小票号saleno  该小票上的10元购物券数量    按促销活动规定该小票应当发放的10元购物券数量 
    A01        2008122601       1                                2 
  A01         2008122702        0                              1 
  A02        2008122703        3                              2 
  A03          2008122904       0                                  0  说明:将数据表1中20081226-20090115期间,小票号相同的金额累加,并由此计算出该小票应发放出的10元购物券数量, 
   将数据表2中小票号相同的10元购物券数量累加,由此得出该小票实际发放出的10元购物券数量, 
   并将上述两个结果,按照小票号相同进行连接 */ 
以下问题尽管已经解决,但因与本题相关,故一并列出 

参见:
难题求教!如何循环使用Sum函数
http://topic.csdn.net/u/20090203/22/a2e5c40e-4d07-471f-ae74-51d12bbeb509.html相关问题1.查出所有需要赠券的小票与该小票的赠券数量(已完成) 
select a.saleno, a.shopid, a.begindate, floor((sum(a.realamt))/50) as giftAmount 
from salerecord a 
where  a.begindate >='2008-12-26' and a. begindate <'2009-01-15' and a.realamt>0 
group by a.saleno, a.shopid, a.begindate 
having floor((sum(a.realamt))/50)>0 
结果如下 
门店号shopid 小票号saleno 价格    日期begindate            赠券数量giftAmount 
A01        2008122601       110.5  2009-12-26 12:33:55:789      2 
A01        2008122702        66      2009-12-27 3:33:55:789        1 
A02        2008122703        110.5  2009-12-27 3:33:55:789        2 

解决方案 »

  1.   

    select 
    a.saleno, 
    a.shopid, 
    a.begindate, 
    bb.giftNum
    floor((sum(a.realamt))/50) as giftAmountfrom salerecord a 
    (select b.saleno, 
    sum(b.数量) as giftNum
    from salegoods b 
    where b.商品名称 = '10元购物券'
    group by b.saleno) bbwhere  
        a.begindate >='2008-12-26' 
    and a. begindate <'2009-01-15' 
    and a.realamt>0 
    and bb.saleno = a.saleno
    group by a.saleno, a.shopid, a.begindate 
    having floor((sum(a.realamt))/50)>0 
    本人没有数据库,不知是否能通过。
    不过,这个思路是可以的。
      

  2.   

    DECLARE @salePrice TABLE 
    (shopid VARCHAR(32),
    saleno VARCHAR(32),
    price DECIMAL(12,1),
    BeginDate DATETIME)INSERT INTO @salePrice 
    SELECT  'A01','200901','11.5', '2009-12-26 12:33:55:789' UNION ALL 
    SELECT        'A01','200901','99' ,  '2009-12-26 12:33:55:789' UNION ALL 
    SELECT        'A01','200902','66' ,  '2009-12-27 3:33:55:789'UNION ALL 
    SELECT        'A02','200903','55' ,  '2009-12-27 3:33:55:789'UNION ALL 
    SELECT        'A02','200903','33' ,  '2009-12-27 3:33:55:789'UNION ALL 
    SELECT        'A02','200903','22.5', '2009-12-27 3:33:55:789'UNION ALL 
    SELECT        'A03','200904','11' ,  '2009-12-29 8:38:55:789'DECLARE @salegoods TABLE 
    (shopid VARCHAR(32),
    saleno VARCHAR(32),
    GoodsName VARCHAR(32),
    Num DECIMAL(12,1))INSERT INTO @salegoods 
    SELECT    'A01',          '200901',    '023手帕纸' ,    '3'UNION ALL 
    SELECT    'A01',          '200901',    '10元购物券',    '1'    UNION ALL 
    SELECT    'A01',          '200902',    '88饼干',        '2.3' UNION ALL 
    SELECT    'A02' ,         '200903',    '51好运鞋'  ,    '7' UNION ALL 
    SELECT    'A03' ,         '200904',    '21熊猫烟'  ,   '3' UNION ALL 
    SELECT    'A02' ,         '200903',    '99洒',    '1' UNION ALL 
    SELECT    'A02','200903','10元购物券','1' UNION ALL 
    SELECT    'A02' ,         '200903',    '10元购物券' ,   '2'  
     
     SELECT DISTINCT
            A.shopid [门店号shopid],
            A.saleno as [小票号saleno],
            c.Num  [该小票上的10元购物券数量],
            D.Num2 [按促销活动规定该小票应当发放的10元购物券数量]
            FROM @salegoods A INNER JOIN @salePrice B
            ON  A.shopid=B.shopid and A.saleno=B.saleno
            INNER JOIN (SELECT shopid,saleno,SUM(Num) Num FROM @salegoods WHERE GoodsName='10元购物券'
     GROUP BY shopid,saleno) C ON A.shopid=C.shopid and A.saleno=C.saleno
            INNER JOIN (SELECT shopid,saleno,SUM(price) Num1, FLOOR(SUM(price)/50) Num2 FROM @salePrice  
     GROUP BY shopid,saleno 
     HAVING SUM(price) > 100) D ON A.shopid=D.shopid and A.saleno=D.saleno
     WHERE b.BeginDate >='2008-12-26' and b. begindate <'2010-12-26'
     
    门店号shopid  小票号saleno   该小票上的10元购物券数量         按促销活动规定该小票应当发放的10元购物券数量A01           200901            1.0                                     2
    A02           200903            3.0                                     2(2 行受影响)SELECT A.shopid [门店号shopid], A.saleno as [小票号saleno], floor((sum(a.price))/50) [该小票上的10元购物券数量], ISNULL(b.Num,0) [按促销活动规定该小票应当发放的10元购物券数量]  FROM @salePrice a LEFT JOIN (SELECT shopid,saleno,SUM(Num) Num FROM @salegoods WHERE GoodsName='10元购物券' GROUP BY shopid,saleno) B ON a. shopid = B.shopid AND a.saleno = b.saleno where a.begindate >='2008-12-26' and a. begindate <'2010-01-15' GROUP BY A.shopid,A.saleno,b.Num 门店号shopid 小票号saleno 该小票上的10元购物券数量 按促销活动规定该小票应当发放的10元购物券数量 A01 200901 2 1.0 A01 200902 1 0.0 A02 200903 2 3.0 A03 200904 0 0.0
      

  3.   

    SQL> select a.shopid, a.saleno, cc, dd
      2    from (select shopid,
      3                 saleno,
      4                 case
      5                   when sum(price) > 50 and sum(price) < 100 then
      6                    1
      7                   when sum(price) < 50 then
      8                    0
      9                   else
     10                    2
     11                 end cc
     12            from saleprice
     13           group by shopid, saleno) a,
     14         (select shopid,
     15                 saleno,
     16                 sum(decode(shopingname, '10元购物券', 1, 0)) dd
     17            from salegoods
     18           group by shopid, saleno) b
     19   where a.shopid = b.shopid
     20     and a.saleno = b.saleno
     21  ;
     
    SHOPID                   SALENO         CC         DD
    -------------------- ---------- ---------- ----------
    A01                  2008122601          2          1
    A01                  2008122702          1          0
    A02                  2008122703          2          2
    A03                  2008122904          0          0