日期       商品ID 商品名称 销量 
2008-11-1 1 衣服 3 
2008-11-1 2 蓝衣服 5 
2008-11-1 3 白衣服 7 
2008-11-1 4 红衣服 3 
2008-11-1 5 裤子 2 
2008-11-1 6 蓝裤子 2 
2008-11-1 7 白裤子 3 
2008-11-2 1 衣服 6 
2008-11-2 2 蓝衣服 8 
2008-11-2 4 红衣服 12 
2008-11-2 5 裤子 4 
2008-11-2 6 蓝裤子 3 
2008-11-2 7 白裤子 5 
表一 
商品ID1 商品ID2 
1 2 
1 3 
1 4 
5 6 
5 7 
表二 求输出结果: 
商品编号 商品名称 日期1销量   日期2销量 
1 衣服 18   26 
5 裤子 7   12 
说明: 
日期1日期2是由人指定的,并且只会给定两个日期 
在输出结果中,表二中ID1有对应的ID2,则统计时,ID2的销量与ID1的销量合计在一起,并以ID1的名字显示出来。 谢谢各位高手了!谢谢 以前发贴:
http://topic.csdn.net/u/20090806/15/1d35edc3-b83c-4692-87af-b0218c85f0ad.html?seed=1606243949&r=58964224#r_58964224
8楼的答案好象是死循环。

解决方案 »

  1.   

    select 
     商品编号 = isnull(b.商品ID1,a.商品ID)
     商品名称 = max(a. 商品名称)
     日期1销量 = sum(case 日期 when '2008-11-1' then a.销量 else 0 end)
     日期2销量 = sum(case 日期 when '2008-11-2' then a.销量 else 0 end)
    from 表一  a left join 表二  b on a.商品ID = b.商品ID2 
    where a.日期 in ('2008-11-1','2008-11-2')     -- 给定两个日期,可以自己修改
    group by isnull(b.商品ID1,a.商品ID)
      

  2.   

    典型的BOM结构 
    蓝衣服,白衣服,红衣服都是衣服的子结点
      

  3.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-10 14:03:54
    ----------------------------------------------------------------
    --> 测试数据:[ta]
    if object_id('[ta]') is not null drop table [ta]
    go
    create table [ta]([日期] datetime,[商品ID] int,[商品名称] varchar(6),[销量] int)
    insert [ta]
    select '2008-11-1',1,'衣服',3 union all
    select '2008-11-1',2,'蓝衣服',5 union all
    select '2008-11-1',3,'白衣服',7 union all
    select '2008-11-1',4,'红衣服',3 union all
    select '2008-11-1',5,'裤子',2 union all
    select '2008-11-1',6,'蓝裤子',2 union all
    select '2008-11-1',7,'白裤子',3 union all
    select '2008-11-2',1,'衣服',6 union all
    select '2008-11-2',2,'蓝衣服',8 union all
    select '2008-11-2',4,'红衣服',12 union all
    select '2008-11-2',5,'裤子',4 union all
    select '2008-11-2',6,'蓝裤子',3 union all
    select '2008-11-2',7,'白裤子',5
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([商品ID1] int,[商品ID2] int)
    insert [tb]
    select 1,2 union all
    select 1,3 union all
    select 1,4 union all
    select 5,6 union all
    select 5,7
    --------------开始查询--------------------------
    select 
       商品编号 = isnull(b.商品ID1,a.商品ID),
       商品名称 = max(a. 商品名称),
       日期1销量 = sum(case 日期 when '2008-11-1' then a.销量 else 0 end),
       日期2销量 = sum(case 日期 when '2008-11-2' then a.销量 else 0 end)
    from 
       ta  a left join tb  b on a.商品ID = b.商品ID2 
    where 
        a.日期 in ('2008-11-1','2008-11-2')    
    group by 
        isnull(b.商品ID1,a.商品ID)
    ----------------结果----------------------------
    /*商品编号        商品名称   日期1销量       日期2销量       
    ----------- ------ ----------- ----------- 
    1           衣服     18          26
    5           蓝裤子    7           12(所影响的行数为 2 行)
    */
      

  4.   

    原用5楼if object_id('products') is not null drop table products
      create table products(rq nvarchar(10),spid int,spmch nvarchar(100),shl decimal(14,2))
    insert into products
    select '2008-11-1', 1, '衣服', 3 
    union  all select '2008-11-1', 2, '蓝衣服', 5 
    union  all select '2008-11-1', 3, '白衣服', 7 
    union  all select '2008-11-1', 4, '红衣服', 3 
    union  all select '2008-11-1', 5, '裤子', 2 
    union  all select '2008-11-1', 6, '蓝裤子', 2 
    union  all select '2008-11-1', 7, '白裤子', 3 
    union  all select '2008-11-2', 1, '衣服', 6 
    union  all select '2008-11-2', 2, '蓝衣服', 8 
    union  all select '2008-11-2', 4, '红衣服', 12 
    union  all select '2008-11-2', 5, '裤子', 4 
    union  all select '2008-11-2', 6, '蓝裤子', 3 
    union  all select '2008-11-2', 7, '白裤子', 5 if object_id('products2') is not null drop table products2
      create table products2(spid1 int,spid2 int)
    insert into products2
    select 1, 2 
    union  all select 1, 3 
    union  all select 1, 4 
    union  all select 5, 6 
    union  all select 5, 7 select
     商品内码 = isnull(b.spid1,a.spid),
     商品名称 = max(a. spmch),
     日期1销量 = sum(case rq when '2008-11-1' then a.shl else 0 end),
     日期2销量 = sum(case rq when '2008-11-2' then a.shl else 0 end)
    from products  a left join products2  b on a.spid = b.spid2 
    where a.rq in ('2008-11-1','2008-11-2')     -- 给定两个日期,可以自己修改
    group by isnull(b.spid1,a.spid)/*
    商品内码 商品名称 日期1销量 日期2销量
    1 衣服 18.00 26.00
    5 蓝裤子 7.00 12.00
    */
      

  5.   

    方法二if object_id('products') is not null drop table products
      create table products(rq nvarchar(10),spid int,spmch nvarchar(100),shl decimal(14,2))
    insert into products
    select '2008-11-1', 1, '衣服', 3 
    union  all select '2008-11-1', 2, '蓝衣服', 5 
    union  all select '2008-11-1', 3, '白衣服', 7 
    union  all select '2008-11-1', 4, '红衣服', 3 
    union  all select '2008-11-1', 5, '裤子', 2 
    union  all select '2008-11-1', 6, '蓝裤子', 2 
    union  all select '2008-11-1', 7, '白裤子', 3 
    union  all select '2008-11-2', 1, '衣服', 6 
    union  all select '2008-11-2', 2, '蓝衣服', 8 
    union  all select '2008-11-2', 4, '红衣服', 12 
    union  all select '2008-11-2', 5, '裤子', 4 
    union  all select '2008-11-2', 6, '蓝裤子', 3 
    union  all select '2008-11-2', 7, '白裤子', 5 if object_id('products2') is not null drop table products2
      create table products2(spid1 int,spid2 int)
    insert into products2
    select 1, 2 
    union  all select 1, 3 
    union  all select 1, 4 
    union  all select 5, 6 
    union  all select 5, 7 
    --使用动态SQL方法
    declare @sql nvarchar(4000)
    set @sql=N'select 商品内码 = isnull(b.spid1,a.spid),商品名称 = max(a. spmch) '
    select @sql=@sql+
                     N','+QUOTENAME(rq+N'销售量')+
                     N'=max(case rq when '+quotename(rq,N'''')+  
                     N' then shl else 0 end)'
           from products  group by rq  
           
    set @sql=@sql+N' from products  a left join products2  b on a.spid = b.spid2 
    group by isnull(b.spid1,a.spid)'
    exec(@sql)
    /*
    商品内码 商品名称 2008-11-1销售量 2008-11-2销售量
    1 衣服 7.00 12.00
    5 蓝裤子 3.00 5.00
    */
      

  6.   

    需要指定日期加上WHERE条件就可以了,至于列名,可以改select语句后面的
      

  7.   

    在输出结果中,表二中ID1有对应的ID2,则统计时,ID2的销量与ID1的销量合计在一起,并以ID1的名字显示出来。 
      

  8.   

    create table tb1(日期 datetime,商品ID int ,商品名称 nvarchar(10),销量 int)insert into tb1
    select '2008-11-1',1,'衣服',3 union all
    select '2008-11-1',2,'蓝衣服',5 union all
    select '2008-11-1',3,'白衣服',7 union all
    select '2008-11-1',4,'红衣服',3 union all
    select '2008-11-1',5,'裤子',2 union all
    select '2008-11-1',6,'蓝裤子',2 union all
    select '2008-11-1',7,'白裤子',3union allselect '2008-11-2',1,'衣服',6 union all
    select '2008-11-2',2,'蓝衣服',8 union all
    select '2008-11-2',4,'红衣服',12 union all
    select '2008-11-2',5,'裤子',4 union all
    select '2008-11-2',6,'蓝裤子',3 union all
    select '2008-11-2',7,'白裤子',5go
    create table tb2(商品ID1 INT,商品ID2 INT)insert into tb2
    select 1,2 union all
    select 1,3 union all
    select 1,4 union all
    select 5,6 union all
    select 5,7 
    go
    select 商品编号=isnull(b.商品ID1,a.商品ID),
    商品名称=max(a.商品名称),
    销量1=sum(case 日期 when '2008-11-1' then a.销量 else 0 end),
    销量2=sum(case 日期 when '2008-11-2' then a.销量 else 0 end)
    from tb1 a left join tb2 b on a.商品ID=b.商品ID2 --where a.日期 in ('2008-11-1','2008-11-2') 
    group by isnull(b.商品ID1,a.商品ID)drop table tb1,tb2抄来学习学习
      

  9.   

    我改成下面的语句,不成功,请帮忙看看错在哪,谢谢!select a.商品ID as 商品络,
           max(a.商品名称) as 商品名称,
          (select sum(销量) from tb1 where 日期 ='2008-11-1') as 日期1销量, 
          (select sum(销量) from tb1 where 日期 ='2008-11-2') as 日期2销量
    from tb1 a left join tb2 b on a商品ID=b.商品ID2
    where a.日期 in ('2008-11-1','2008-11-2')
    Group by a.商品ID