最近写的动态行列转换总不成功.即便照着别人的都不行,为什么?快神经了!
/*-- 数据测试环境 --*/
if exists (select * from dbo.sysobjects where id = object_id(N'[tb]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tb]
GOcreate table tb(dwmch varchar(10),rq datetime,xshe int)
insert into tb
select 'A单位','2001-01-01',100
union all select 'B单位','2001-01-02',101
union all select 'C单位','2001-01-03',102
union all select 'D单位','2001-01-04',103
union all select 'E单位','2001-01-05',104
union all select 'F单位','2001-01-06',105
union all select 'G单位','2001-01-07',106
union all select 'H单位','2001-01-08',107
union all select 'I单位','2001-01-09',108
union all select 'J单位','2001-01-11',109/*-- 要求结果
日期       A单位  B单位 C单位 D单位 E单位  F单位 G单位 H单位 I单位 J单位   
---------- ----- ----- ----- ----- ----- ----- ----  ----  ---- ------
2001-01-01 100   0     0     0     0     0     0     0     0     0
2001-01-02 0     101   0     0     0     0     0     0     0     0
2001-01-03 0     0     102   0     0     0     0     0     0     0
2001-01-04 0     0     0     103   0     0     0     0     0     0
2001-01-05 0     0     0     0     104   0     0     0     0     0
2001-01-06 0     0     0     0     0     105   0     0     0     0
2001-01-07 0     0     0     0     0     0     106   0     0     0
2001-01-08 0     0     0     0     0     0     0     107   0     0
2001-01-09 0     0     0     0     0     0     0     0     108   0
2001-01-11 0     0     0     0     0     0     0     0     0     109
--*/-- 常规处理方法 
declare @sql varchar(8000)
set @sql='select rq=convert(varchar(10),rq,120)'
select @sql=@sql+',['+dwmch+']=sum(case dwmch when '''+dwmch+''' then xshe else 0 end)'from(select distinct dwmch from tb) a
exec(@sql+' from tb group by convert(varchar(10),rq,120)')--我的方法,只不过是把@sql改成@a,为什么就不对呢?绝对没有使用中文符号
declare @a varchar(8000)
set @a='select rq=convert(varchar(10),rq,120)'
set @a=@a+' ,['+dwmch+']=sum(case dwmch when '''+dwmch+''' then xshe else 0 end)' from (select distinct dwmch from tb) a
exec(@a+' from tb group by convert(varchar(10).rq,120)')

解决方案 »

  1.   

    --这样试一下
    declare @a varchar(8000)
    set @a='select rq=convert(varchar(10),rq,120)'
    select @a=@a+',['+dwmch+']=sum(case dwmch when '''+dwmch+''' then xshe else 0 end)'from(select distinct dwmch from tb) a
    exec ( @a+' from tb group by convert(varchar(10),rq,120)')
      

  2.   

    错误信息?是不是你的数据不同的dwmch 太多,超出了varchar(8000)的范围
      

  3.   

    兄弟,我的语句和你的除了最后EXEC里CONVERT里的一个符号有错误,刚才我改正了执行,还是错误的,以外,和你的一个符号都不差,为什么我的总出错?
    你能否把我的程序拷贝过去执行下,然后告诉我错误在什么地方呢?谢谢.
    搞了半天也有错误
      

  4.   

    知道了,谢谢世界杯啊!
    SET 和SELECT 都是赋值啊,为什么在动态语句中不同呢?
      

  5.   

    set @a=@a+' ,['+dwmch+']=sum(case dwmch when '''+dwmch+''' then xshe else 0 end)' from (select distinct dwmch from tb) a---这句话是给@a赋值。根据表里的值给变量赋值set就不行了
      

  6.   

    /*编号       货位        数量
    1001        A           10
    1001        B           20
    1001        C           20
    1002        A           10
    1002        B           50
    1003        A           10
    -------------------返回的结果是:
    编号     A      B      C
    1001     10     20     20
    1002     10     50     NULL or 0
    1003     10     null or 0 null or 0
    */
    create table t(spbh varchar(4),hw varchar(1),shl int)
    insert t values('1001','a',10)
    insert t values('1001','b',20)
    insert t values('1001','c',20)
    insert t values('1002','a',10)
    insert t values('1002','b',50)
    insert t values('1003','a',10)--静态的,可以正确执行
    --select spbh,sum(case when hw='a' then shl else 0 end) as a,sum(case when hw='b' then shl else 0 end) as b,sum(case when hw='c' then shl else 0 end) as c from t group by spbh
    --动态的,不停的报错!
    declare @sql varchar(8000),@last varchar(8000)
    select  @sql='select spbh'
    select  @last=',['+hw+']=sum(case when spbh= '''+hw+''' then shl else 0 end)' from (select distinct hw from t) aselect  @sql=@sql+@last+'from t group by spbh'
    exec(@sql+@last)
    print @sql
    drop table t
      

  7.   

    兄弟,你没上QQ啊?那帮我再看看最后的动态的,把SET 改成SELECT 了,怎么还报错啊
      

  8.   

    create table t(spbh varchar(4),hw varchar(1),shl int)
    insert t values('1001','a',10)
    insert t values('1001','b',20)
    insert t values('1001','c',20)
    insert t values('1002','a',10)
    insert t values('1002','b',50)
    insert t values('1003','a',10)--静态的,可以正确执行
    --select spbh,sum(case when hw='a' then shl else 0 end) as a,sum(case when hw='b' then shl else 0 end) as b,sum(case when hw='c' then shl else 0 end) as c from t group by spbh
    --动态的,不停的报错!
    declare @sql varchar(8000),@last varchar(8000)
    select  @sql='select spbh'
    set @last=''
    select  @last=@last+',['+hw+']=sum(case when hw= '''+hw+''' then shl else 0 end)' from (select distinct hw from t)a
    exec(@sql+@last+' from t group by spbh')drop table t
      

  9.   

    declare @sql varchar(8000),@last varchar(8000)
    select  @sql='select spbh'
    select  @last=',['+hw+']=sum(case when spbh= '''+hw+''' then shl else 0 end) ' from t group by hw
    select  @last=@last+'from t group by spbh'
    exec(@sql+@last)
    print @sql这次怎么只是最后一个值的查询结果?
      

  10.   

    没有累加!
    select  @last=@last+',['+hw+']=sum(case when spbh= '''+hw+''' then shl else 0 end) ' from t group by hw--@last没累加
      

  11.   

    问题在这里
    @last需要初始化select  @last=',['+hw+']=sum(case when spbh= '''+hw+''' then shl else 0 end) ' from t group by hw--上面的,没有把每条hw产生的语句连接起来。应改为
    select @last=@last+',['+hw+']=sum(case when spbh= '''+hw+''' then shl else 0 end) ' from t group by hw
      

  12.   

    好了,多谢兄弟,我看贴迟了一步.
    对了,能否把(select disdtinct hw from t) a换成别的呢?
    我看别人写的都不是这样的,直接用 from t group by 什么的?
      

  13.   

    create table t(spbh varchar(4),hw varchar(1),shl int)
    insert t values('1001','a',10)
    insert t values('1001','b',20)
    insert t values('1001','c',20)
    insert t values('1002','a',10)
    insert t values('1002','b',50)
    insert t values('1003','a',10)--静态的,可以正确执行
    --select spbh,sum(case when hw='a' then shl else 0 end) as a,sum(case when hw='b' then shl else 0 end) as b,sum(case when hw='c' then shl else 0 end) as c from t group by spbh
    --动态的,不停的报错!
    declare @sql varchar(8000),@last varchar(8000)
    select  @sql='select spbh'
    set @last=''
    select  @last=@last+',['+hw+']=sum(case when hw= '''+hw+''' then shl else 0 end)' from t group by hw
    exec(@sql+@last+' from t group by spbh')drop table t