最近写的动态行列转换总不成功.即便照着别人的都不行,为什么?快神经了!
/*-- 数据测试环境 --*/
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)')
/*-- 数据测试环境 --*/
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)')
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)')
你能否把我的程序拷贝过去执行下,然后告诉我错误在什么地方呢?谢谢.
搞了半天也有错误
SET 和SELECT 都是赋值啊,为什么在动态语句中不同呢?
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
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
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这次怎么只是最后一个值的查询结果?
select @last=@last+',['+hw+']=sum(case when spbh= '''+hw+''' then shl else 0 end) ' from t group by hw--@last没累加
@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
对了,能否把(select disdtinct hw from t) a换成别的呢?
我看别人写的都不是这样的,直接用 from t group by 什么的?
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