表名  送货表,字段为:  送货日期,送货方,生产线,轧辊名称,送货数量。要实现SQL 如下
送货方 生产线 轧辊名称  1日送货数量  2日送货数量  3日送货数量 。。31日送货数量如果当天没有就显示0) 总计(这个轧辊总计)

解决方案 »

  1.   

    典型的行列转换问题http://topic.csdn.net/u/20080614/17/22e73f33-f071-46dc-b9bf-321204b1656f.html
      

  2.   

    表名  送货表,字段为:  送货日期,送货方,生产线,轧辊名称,送货数量。 要实现SQL 如下 
    送货方 生产线 轧辊名称  1日送货数量  2日送货数量  3日送货数量 。。31日送货数量如果当天没有就显示0) 总计(这个轧辊总计) 
    --1 、不怕麻烦就
    select 送货方,生产线 ,轧辊名称,convert(char(7), 送货日期,120)
      1日送货数量 = sum(case when datepart(d,送货日期) = 1 then 送货数量 else 0 end),
      2日送货数量 = sum(case when datepart(d,送货日期) = 2 then 送货数量 else 0 end),
    ...
      31日送货数量= sum(case when datepart(d,送货日期) = 31 then 送货数量 else 0 end),
    from ta
    group by 送货方,生产线 ,轧辊名称,convert(char(7), 送货日期,120)
      

  3.   

    2、动态SQLdeclare @s varchar(8000)
    select @s = isnull(@s+',','')+'['+d+'日送货数量] = sum(case when datepart(d,送货日期) = '+ltrim (d)+' then 送货数量 else 0 end)'
    from (select distinct datepart(d,送货日期) as d from ta ) aexec('select 送货方,生产线 ,轧辊名称,convert(char(7), 送货日期,120) ,'+@s+ ' from ta 
    group by 送货方,生产线 ,轧辊名称,convert(char(7), 送货日期,120) ')
      

  4.   

    select 送货方,生产线,轧辊名称,
      isnull(max(case day(送货日期) when 1 then 送货数量 end),0) as [1日送货数量],
      isnull(max(case day(送货日期) when 2 then 送货数量 end),0) as [2日送货数量],
      isnull(max(case day(送货日期) when 3 then 送货数量 end),0) as [3日送货数量],
      isnull(max(case day(送货日期) when 4 then 送货数量 end),0) as [4日送货数量],
      isnull(max(case day(送货日期) when 5 then 送货数量 end),0) as [5日送货数量],
      isnull(max(case day(送货日期) when 6 then 送货数量 end),0) as [6日送货数量],
      isnull(max(case day(送货日期) when 7 then 送货数量 end),0) as [7日送货数量],
      isnull(max(case day(送货日期) when 8 then 送货数量 end),0) as [8日送货数量],
      isnull(max(case day(送货日期) when 9 then 送货数量 end),0) as [9日送货数量],
      isnull(max(case day(送货日期) when 10 then 送货数量 end),0) as [10日送货数量],
      isnull(max(case day(送货日期) when 11 then 送货数量 end),0) as [11日送货数量],
      isnull(max(case day(送货日期) when 12 then 送货数量 end),0) as [12日送货数量],
      isnull(max(case day(送货日期) when 13 then 送货数量 end),0) as [13日送货数量],
      isnull(max(case day(送货日期) when 14 then 送货数量 end),0) as [14日送货数量],
      isnull(max(case day(送货日期) when 15 then 送货数量 end),0) as [15日送货数量],
      isnull(max(case day(送货日期) when 16 then 送货数量 end),0) as [16日送货数量],
      isnull(max(case day(送货日期) when 17 then 送货数量 end),0) as [17日送货数量],
      isnull(max(case day(送货日期) when 18 then 送货数量 end),0) as [18日送货数量],
      isnull(max(case day(送货日期) when 19 then 送货数量 end),0) as [19日送货数量],
      isnull(max(case day(送货日期) when 20 then 送货数量 end),0) as [20日送货数量],
      isnull(max(case day(送货日期) when 21 then 送货数量 end),0) as [21日送货数量],
      isnull(max(case day(送货日期) when 22 then 送货数量 end),0) as [22日送货数量],
      isnull(max(case day(送货日期) when 23 then 送货数量 end),0) as [23日送货数量],
      isnull(max(case day(送货日期) when 24 then 送货数量 end),0) as [24日送货数量],
      isnull(max(case day(送货日期) when 25 then 送货数量 end,0)) as [25日送货数量],
      isnull(max(case day(送货日期) when 26 then 送货数量 end),0) as [26日送货数量],
      isnull(max(case day(送货日期) when 27 then 送货数量 end),0) as [27日送货数量],
      isnull(max(case day(送货日期) when 28 then 送货数量 end),0) as [28日送货数量],
      isnull(max(case day(送货日期) when 29 then 送货数量 end),0) as [29日送货数量],
      isnull(max(case day(送货日期) when 30 then 送货数量 end),0) as [30日送货数量],
      isnull(max(case day(送货日期) when 31 then 送货数量 end),0) as [31日送货数量],
      总计=isnull((select sum(送货数量) from 送货表 where 送货方=a.送货方 and 生产线=a.生产线 and 轧辊名称=a.轧辊名称),0)
    from 送货表 a
    这样?
      

  5.   


    group by 送货方,生产线 ,轧辊名称,convert(char(7), 送货日期,120) ')最后是yyyy-mm分组
      

  6.   

    select 送货方,生产线,轧辊名称,
      isnull(max(case day(送货日期) when 1 then 送货数量 end),0) as [1日送货数量],
      isnull(max(case day(送货日期) when 2 then 送货数量 end),0) as [2日送货数量],
      isnull(max(case day(送货日期) when 3 then 送货数量 end),0) as [3日送货数量],
      isnull(max(case day(送货日期) when 4 then 送货数量 end),0) as [4日送货数量],
      isnull(max(case day(送货日期) when 5 then 送货数量 end),0) as [5日送货数量],
      isnull(max(case day(送货日期) when 6 then 送货数量 end),0) as [6日送货数量],
      isnull(max(case day(送货日期) when 7 then 送货数量 end),0) as [7日送货数量],
      isnull(max(case day(送货日期) when 8 then 送货数量 end),0) as [8日送货数量],
      isnull(max(case day(送货日期) when 9 then 送货数量 end),0) as [9日送货数量],
      isnull(max(case day(送货日期) when 10 then 送货数量 end),0) as [10日送货数量],
      isnull(max(case day(送货日期) when 11 then 送货数量 end),0) as [11日送货数量],
      isnull(max(case day(送货日期) when 12 then 送货数量 end),0) as [12日送货数量],
      isnull(max(case day(送货日期) when 13 then 送货数量 end),0) as [13日送货数量],
      isnull(max(case day(送货日期) when 14 then 送货数量 end),0) as [14日送货数量],
      isnull(max(case day(送货日期) when 15 then 送货数量 end),0) as [15日送货数量],
      isnull(max(case day(送货日期) when 16 then 送货数量 end),0) as [16日送货数量],
      isnull(max(case day(送货日期) when 17 then 送货数量 end),0) as [17日送货数量],
      isnull(max(case day(送货日期) when 18 then 送货数量 end),0) as [18日送货数量],
      isnull(max(case day(送货日期) when 19 then 送货数量 end),0) as [19日送货数量],
      isnull(max(case day(送货日期) when 20 then 送货数量 end),0) as [20日送货数量],
      isnull(max(case day(送货日期) when 21 then 送货数量 end),0) as [21日送货数量],
      isnull(max(case day(送货日期) when 22 then 送货数量 end),0) as [22日送货数量],
      isnull(max(case day(送货日期) when 23 then 送货数量 end),0) as [23日送货数量],
      isnull(max(case day(送货日期) when 24 then 送货数量 end),0) as [24日送货数量],
      isnull(max(case day(送货日期) when 25 then 送货数量 end,0)) as [25日送货数量],
      isnull(max(case day(送货日期) when 26 then 送货数量 end),0) as [26日送货数量],
      isnull(max(case day(送货日期) when 27 then 送货数量 end),0) as [27日送货数量],
      isnull(max(case day(送货日期) when 28 then 送货数量 end),0) as [28日送货数量],
      isnull(max(case day(送货日期) when 29 then 送货数量 end),0) as [29日送货数量],
      isnull(max(case day(送货日期) when 30 then 送货数量 end),0) as [30日送货数量],
      isnull(max(case day(送货日期) when 31 then 送货数量 end),0) as [31日送货数量],
      总计=isnull((select sum(送货数量) from 送货表 where 送货方=a.送货方 and 生产线=a.生产线 and 轧辊名称=a.轧辊名称),0)
    from 送货表 a
    group by 送货方,生产线,轧辊名称
      

  7.   

    select 送货方,生产线 ,轧辊名称,convert(char(7), 送货日期,120) 
      1日送货数量 = sum(case when datepart(d,送货日期) = 1 then 送货数量 else 0 end), 
      2日送货数量 = sum(case when datepart(d,送货日期) = 2 then 送货数量 else 0 end), 
    ... 
      31日送货数量= sum(case when datepart(d,送货日期) = 31 then 送货数量 else 0 end), 
    from ta 
    group by 送货方,生产线 ,轧辊名称,convert(char(7), 送货日期,120) 
      

  8.   

    哥们  总计=isnull((select sum(送货数量) from 送货表 where 送货方=a.送货方 and 生产线=a.生产线 and 轧辊名称=a.轧辊名称),0) 这个查询德结果是 0  不对!
      
      

  9.   

    ---测试数据---
    create table 送货表(送货方 varchar(20),生产线 varchar(20),轧辊名称 varchar(20),送货日期 datetime,送货数量 int)
    insert 送货表
    select 'A','B','C','2008-11-01',10 union all
    select 'A','B','C','2008-11-02',20 union all
    select 'A','B','C','2008-11-03',30 union all
    select 'A','B','C','2008-11-04',10 union all
    select 'A','B','C','2008-11-05',80 union all
    select 'A','B','C','2008-11-06',10 union all
    select 'A','B','C','2008-11-07',30 union all
    select 'A','B','C','2008-11-08',35 union all
    select 'X','Y','Z','2008-11-11',15 union all
    select 'X','Y','Z','2008-11-12',10 union all
    select 'X','Y','Z','2008-11-13',20 union all
    select 'X','Y','Z','2008-11-14',10 union all
    select 'X','Y','Z','2008-11-15',40 union all
    select 'X','Y','Z','2008-11-16',15 union all
    select 'X','Y','Z','2008-11-17',30 union all
    select 'X','Y','Z','2008-11-18',35  ---查询---
    select 送货方,生产线,轧辊名称,
      isnull(max(case day(送货日期) when 1 then 送货数量 end),0) as [1日送货数量],
      isnull(max(case day(送货日期) when 2 then 送货数量 end),0) as [2日送货数量],
      isnull(max(case day(送货日期) when 3 then 送货数量 end),0) as [3日送货数量],
      isnull(max(case day(送货日期) when 4 then 送货数量 end),0) as [4日送货数量],
      isnull(max(case day(送货日期) when 5 then 送货数量 end),0) as [5日送货数量],
      isnull(max(case day(送货日期) when 6 then 送货数量 end),0) as [6日送货数量],
      isnull(max(case day(送货日期) when 7 then 送货数量 end),0) as [7日送货数量],
      isnull(max(case day(送货日期) when 8 then 送货数量 end),0) as [8日送货数量],
      isnull(max(case day(送货日期) when 9 then 送货数量 end),0) as [9日送货数量],
      isnull(max(case day(送货日期) when 10 then 送货数量 end),0) as [10日送货数量],
      isnull(max(case day(送货日期) when 11 then 送货数量 end),0) as [11日送货数量],
      isnull(max(case day(送货日期) when 12 then 送货数量 end),0) as [12日送货数量],
      isnull(max(case day(送货日期) when 13 then 送货数量 end),0) as [13日送货数量],
      isnull(max(case day(送货日期) when 14 then 送货数量 end),0) as [14日送货数量],
      isnull(max(case day(送货日期) when 15 then 送货数量 end),0) as [15日送货数量],
      isnull(max(case day(送货日期) when 16 then 送货数量 end),0) as [16日送货数量],
      isnull(max(case day(送货日期) when 17 then 送货数量 end),0) as [17日送货数量],
      isnull(max(case day(送货日期) when 18 then 送货数量 end),0) as [18日送货数量],
      isnull(max(case day(送货日期) when 19 then 送货数量 end),0) as [19日送货数量],
      isnull(max(case day(送货日期) when 20 then 送货数量 end),0) as [20日送货数量],
      isnull(max(case day(送货日期) when 21 then 送货数量 end),0) as [21日送货数量],
      isnull(max(case day(送货日期) when 22 then 送货数量 end),0) as [22日送货数量],
      isnull(max(case day(送货日期) when 23 then 送货数量 end),0) as [23日送货数量],
      isnull(max(case day(送货日期) when 24 then 送货数量 end),0) as [24日送货数量],
      isnull(max(case day(送货日期) when 25 then 送货数量 end),0) as [25日送货数量],
      isnull(max(case day(送货日期) when 26 then 送货数量 end),0) as [26日送货数量],
      isnull(max(case day(送货日期) when 27 then 送货数量 end),0) as [27日送货数量],
      isnull(max(case day(送货日期) when 28 then 送货数量 end),0) as [28日送货数量],
      isnull(max(case day(送货日期) when 29 then 送货数量 end),0) as [29日送货数量],
      isnull(max(case day(送货日期) when 30 then 送货数量 end),0) as [30日送货数量],
      isnull(max(case day(送货日期) when 31 then 送货数量 end),0) as [31日送货数量],
      总计=isnull((select sum(送货数量) from 送货表 where 送货方=a.送货方 and 生产线=a.生产线 and 轧辊名称=a.轧辊名称),0)
    from 送货表 a
    group by 送货方,生产线,轧辊名称---结果---
    送货方                  生产线                  轧辊名称                 1日送货数量      2日送货数量      3日送货数量      4日送货数量      5日送货数量      6日送货数量      7日送货数量      8日送货数量      9日送货数量      10日送货数量     11日送货数量     12日送货数量     13日送货数量     14日送货数量     15日送货数量     16日送货数量     17日送货数量     18日送货数量     19日送货数量     20日送货数量     21日送货数量     22日送货数量     23日送货数量     24日送货数量     25日送货数量     26日送货数量     27日送货数量     28日送货数量     29日送货数量     30日送货数量     31日送货数量     总计          
    -------------------- -------------------- -------------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- 
    A                    B                    C                    10          20          30          10          80          10          30          35          0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           0           225
    X                    Y                    Z                    0           0           0           0           0           0           0           0           0           0           15          10          20          10          40          15          30          35          0           0           0           0           0           0           0           0           0           0           0           0           0           175(所影响的行数为 2 行)貌似没有错...