各位,新年好!
原表如下:
XH CJX CJ
012345678901 X100 88.00
012345678901 Y200 45.00
012345678901 Z500 106.05
012345678902 X100 100.00
012345678902 Z300 90.00
....想把上表中的数据换成如下(里面不能有null,CJX有很多的):
xh X100 Y200 Z300 Z500
012345678901 88.00 45.00 106.05
012345678902 100.00 90.00 用如下行列转换代码为什么错误?
IF object_id('tb') is not null
DROP TABLE tb
go
create table tb(xh char(12) ,cjx char(10), cj numeric(6,2))
insert into tb values ('012345678901','X100',88.00)
insert into tb values ('012345678901','Y200',45.00)
insert into tb values ('012345678901','Z500',106.05)
insert into tb values ('012345678902','X100',100.00)
insert into tb values ('012345678902','Z300',90.00)
-- select * from tbdeclare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when cjx='''+cjx+''' then cj else '''' end) as '+cjx
from tb
group by cjxselect @sql='select xh'+@sql+' into Temp from tb group by xh'
exec(@sql)
-- select * from Temp
提示:从数据类型 varchar 转换为 numeric 时出错。
原表如下:
XH CJX CJ
012345678901 X100 88.00
012345678901 Y200 45.00
012345678901 Z500 106.05
012345678902 X100 100.00
012345678902 Z300 90.00
....想把上表中的数据换成如下(里面不能有null,CJX有很多的):
xh X100 Y200 Z300 Z500
012345678901 88.00 45.00 106.05
012345678902 100.00 90.00 用如下行列转换代码为什么错误?
IF object_id('tb') is not null
DROP TABLE tb
go
create table tb(xh char(12) ,cjx char(10), cj numeric(6,2))
insert into tb values ('012345678901','X100',88.00)
insert into tb values ('012345678901','Y200',45.00)
insert into tb values ('012345678901','Z500',106.05)
insert into tb values ('012345678902','X100',100.00)
insert into tb values ('012345678902','Z300',90.00)
-- select * from tbdeclare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when cjx='''+cjx+''' then cj else '''' end) as '+cjx
from tb
group by cjxselect @sql='select xh'+@sql+' into Temp from tb group by xh'
exec(@sql)
-- select * from Temp
提示:从数据类型 varchar 转换为 numeric 时出错。
set @sql=''
select @sql=@sql+',max(case when cjx='''+cjx+''' then ltrim(cj) else '''' end) as '+cjx
from tb
group by cjxselect @sql='select xh'+@sql+' into Temp from tb group by xh'
exec(@sql)
-- select * from Temp
/**
xh X100 Y200 Z300 Z500
------------ ---------------------------------------- ---------------------------------------- ---------------------------------------- ----------------------------------------
012345678901 88.00 45.00 106.05
012345678902 100.00 90.00 (所影响的行数为 2 行)
**/
drop table temp
insert into tb1 values ('012345678901','X100',88.00)
insert into tb1 values ('012345678901','Y200',45.00)
insert into tb1 values ('012345678901','Z500',106.05)
insert into tb1 values ('012345678902','X100',100.00)
insert into tb1 values ('012345678902','Z300',90.00)
-- select * from tb declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when cjx='''+cjx+''' then ltrim(cj) else '''' end) as '+cjx
from tb1
group by cjx
select @sql='select xh'+@sql+' into Temp1 from tb1 group by xh'
exec(@sql) select * from Temp1
xh X100 Y200 Z300 Z500
------------ ----------------------------------------- ----------------------------------------- ----------------------------------------- -----------------------------------------
012345678901 88.00 45.00 106.05
012345678902 100.00 90.00 (2 行受影响)
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when cjx='''+cjx+''' then cast(cj as varchar(10)) else '''' end) as '+cjx
from tb
group by cjx select @sql='select xh'+@sql+' into Temp from tb group by xh'
exec(@sql)