libin_ftsafe 您好
看了需多关于交叉表的问题,您的答案很好,我现在遇到一个问题,请您帮忙。SQL有个MO表
产品编号 排工日期   排工数量
CP001   08-01-01  1000
CP001   08-01-02  1000
CP002   08-01-01  1000
CP002   08-01-02  1000
...     ...       ...
CP001   08-02-01  1000
需要实现一个月排工计划表,格式为
产品编号  1月1日  1月2日   ...  1月31日
CP001    1000   1000    ...  1000
CP002    1000   1000    ...  1000
CP003    1000   1000    ...  1000应该如何实现,请您抽空帮忙,不胜感激!

解决方案 »

  1.   

    --建立测试环境
    set nocount on
    create table test(产品编号 varchar(20),排工日期 datetime,排工数量 varchar(20))
    insert into test select 'CP001','08-01-01','1000'
    insert into test select 'CP001','08-01-02','1000'
    insert into test select 'CP002','08-01-01','1000'
    insert into test select 'CP002','08-01-02','1000'
    insert into test select 'CP001','08-02-01','1000'
    go
    --测试
    declare @sql varchar(8000)
    set @sql='select 产品编号 '
    select @sql=@sql+',sum(case when 排工日期='''+convert(varchar(10),排工日期,120)+''' then  排工数量 else 0 end) ['+
    cast(month(排工日期) as varchar(2))+'月'+cast(day(排工日期) as varchar(2))+'日]'
     from (select distinct 排工日期 from test)a
    set @sql=@sql+' from test group by 产品编号'exec (@sql)
    --删除测试环境
    drop table test
     set nocount off
      

  2.   

    select  [产品编号],
            sum(case when right(2)='01' then [排工数量] else 0 end) as 1月1日,
            sum(case when right(2)='02' then [排工数量] else 0 end) as 1月2日,
            sum(case when right(2)='03' then [排工数量] else 0 end) as 1月3日,
            sum(case when right(2)='04' then [排工数量] else 0 end) as 1月4日,
            sum(case when right(2)='05' then [排工数量] else 0 end) as 1月5日,
            sum(case when right(2)='06' then [排工数量] else 0 end) as 1月6日,
            sum(case when right(2)='07' then [排工数量] else 0 end) as 1月7日,
            sum(case when right(2)='08' then [排工数量] else 0 end) as 1月8日,
            sum(case when right(2)='09' then [排工数量] else 0 end) as 1月9日,
            sum(case when right(2)='10' then [排工数量] else 0 end) as 1月10日,
            sum(case when right(2)='11' then [排工数量] else 0 end) as 1月11日,
            sum(case when right(2)='12' then [排工数量] else 0 end) as 1月12日,
            sum(case when right(2)='13' then [排工数量] else 0 end) as 1月13日,
            sum(case when right(2)='14' then [排工数量] else 0 end) as 1月14日,
            sum(case when right(2)='15' then [排工数量] else 0 end) as 1月15日,
            sum(case when right(2)='16' then [排工数量] else 0 end) as 1月16日,
            sum(case when right(2)='17' then [排工数量] else 0 end) as 1月17日,
            sum(case when right(2)='18' then [排工数量] else 0 end) as 1月18日,
            sum(case when right(2)='19' then [排工数量] else 0 end) as 1月19日,
            sum(case when right(2)='20' then [排工数量] else 0 end) as 1月20日,
            sum(case when right(2)='21' then [排工数量] else 0 end) as 1月21日,
            sum(case when right(2)='22' then [排工数量] else 0 end) as 1月22日,
            sum(case when right(2)='23' then [排工数量] else 0 end) as 1月23日,
            sum(case when right(2)='24' then [排工数量] else 0 end) as 1月24日,
            sum(case when right(2)='25' then [排工数量] else 0 end) as 1月25日,
            sum(case when right(2)='26' then [排工数量] else 0 end) as 1月26日,
            sum(case when right(2)='27' then [排工数量] else 0 end) as 1月27日,
            sum(case when right(2)='28' then [排工数量] else 0 end) as 1月28日,
            sum(case when right(2)='29' then [排工数量] else 0 end) as 1月29日,
            sum(case when right(2)='30' then [排工数量] else 0 end) as 1月30日,
            sum(case when right(2)='31' then [排工数量] else 0 end) as 1月31日
    from    [MO]
    where   Left([排工日期],5) = '08-01'
    group by [产品编号]
      

  3.   

    首先感谢 jinjazz 和 unsigned 的分享jinjazz 方法的结果是 产品编号  1月1日  1月2日   2月1日 
    CP001    1000   1000    1000 
    CP002    1000   1000    1000 
    CP003    1000   1000    1000
    虽然没有产生报表的格式,但我也是非常感谢。unsigned 方法的结果能显示
    产品编号  1月1日  1月2日   1月3日   ...  1月31日 
    CP001    1000   1000       0    ...  1000 
    CP002    1000   1000       0    ...  1000 
    CP003    1000   1000       0    ...  1000
      

  4.   

    unsigned
    和我原来的想法类似,但仅限于1月份。按这种思路,我需要先做4个SQL语句,一个是1号到31号的,一个是1号到30号的,一个是1号到28号的,一个是1号到29号的,在查询前,先判断需要查询的月份月底的一天是几号,然后选择相应的SQL查询语句,才能达到以下效果
    1月报表是
    产品编号  1月1日  1月2日   1月3日   ...  1月31日 
    CP001    1000   1000       0    ...  1000 
    CP002    1000   1000       0    ...  1000 
    CP003    1000   1000       0    ...  1000
    2月报表是
    产品编号  2月1日  2月2日   2月3日   ...  2月29日 
    CP001    1000   1000       0    ...  1000 
    CP002    1000   1000       0    ...  1000 
    CP003    1000   1000       0    ...  1000
    4月报表是
    产品编号  4月1日  4月2日   4月3日   ...  4月30日 
    CP001    1000   1000       0    ...  1000 
    CP002    1000   1000       0    ...  1000 
    CP003    1000   1000       0    ...  1000不知道大家有更好的办法实现上述要求吗,大家探讨一下。
      

  5.   

    或者说你可以避免不用判断月份吗?生成一个28天的,有29号就增加一个
    ',sum(case when right(2)='29' then [排工数量] else 0 end) as '+Month+'月29日';
    如果再有30号,再增加
    ',sum(case when right(2)='30' then [排工数量] else 0 end) as '+Month+'月30日';
    如果再有31号,再继续增加
    ',sum(case when right(2)='31' then [排工数量] else 0 end) as '+Month+'月31日';