serialno parameter value
--------------------------------------
LJ832785QA Voltage 0.2179481
LJ832785QA Current 2.920481
LJ832785QA Resistance 74.62748
请问能否用SQL语句得到一个如下的记录集?
serialno Voltage Current Resistance
---------------------------------------------------
LJ832785QA 0.2179481 2.920481 74.62748
请帮忙,谢谢!
8 个解决方案
#1
example:
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go
--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go
--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go
--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go
--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
#2
libin_ftsafe(子陌红尘),谢谢你的回复,但我觉得你提供的跟我所要求的不是一个意思啊。
#3
--测试环境
create table #T(serialno varchar(20),parameter varchar(20),value float)
insert into #T select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
--动态SQL
declare @s varchar(2000)
select @s='select serialno '
select @s=@s+',['+parameter+']=sum(case when parameter='''+parameter+''' then value end) '
from #T
Group by parameter
set @s=@s+' From #T group by serialno'
exec(@s)
--结果
serialno Current Resistance Voltage
-------------------------------------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
--删除测试环境
Drop table #T
create table #T(serialno varchar(20),parameter varchar(20),value float)
insert into #T select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
--动态SQL
declare @s varchar(2000)
select @s='select serialno '
select @s=@s+',['+parameter+']=sum(case when parameter='''+parameter+''' then value end) '
from #T
Group by parameter
set @s=@s+' From #T group by serialno'
exec(@s)
--结果
serialno Current Resistance Voltage
-------------------------------------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
--删除测试环境
Drop table #T
#4
抱歉,看错了,以为是字符串求和,更正如下:
declare @s varchar(8000)
set @s = ''
select @s = @s+',['+parameter+']=sum(case parameter when '''+parameter+''' then value end)' from 表 group by parameter
set @s = 'select serialno'+@s+' from 表 group by serialno'
exec(@s)
declare @s varchar(8000)
set @s = ''
select @s = @s+',['+parameter+']=sum(case parameter when '''+parameter+''' then value end)' from 表 group by parameter
set @s = 'select serialno'+@s+' from 表 group by serialno'
exec(@s)
#5
可不可以用游标和表变量啊?
#6
如果parameter固定只有三个值,用静态SQL语句:
select
serialno,
Voltage = sum(case parameter when 'Voltage' then value end),
Current = sum(case parameter when 'Current' then value end),
Resistance = sum(case parameter when 'Resistance' then value end)
from
表
group by
serialno
select
serialno,
Voltage = sum(case parameter when 'Voltage' then value end),
Current = sum(case parameter when 'Current' then value end),
Resistance = sum(case parameter when 'Resistance' then value end)
from
表
group by
serialno
#7
create table A
(
serialno varchar(20),
parameter varchar(20),
value float
)
insert A
select 'LJ832785QA', 'Voltage', 0.2179481 union
select 'LJ832785QA', 'Current', 2.920481 union
select 'LJ832785QA', 'Resistance', 74.62748
--查询
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when parameter='''+parameter+''' then value else 0 end) as '''+parameter+''''
from A group by parameter
select @sql='select serialno'+@sql+' from A group by serialno'
exec(@sql)
--删除测试环境
drop table A
--结果
/*
serialno Current Resistance Voltage
-------------------- -----------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
*/
(
serialno varchar(20),
parameter varchar(20),
value float
)
insert A
select 'LJ832785QA', 'Voltage', 0.2179481 union
select 'LJ832785QA', 'Current', 2.920481 union
select 'LJ832785QA', 'Resistance', 74.62748
--查询
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when parameter='''+parameter+''' then value else 0 end) as '''+parameter+''''
from A group by parameter
select @sql='select serialno'+@sql+' from A group by serialno'
exec(@sql)
--删除测试环境
drop table A
--结果
/*
serialno Current Resistance Voltage
-------------------- -----------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
*/
#8
create table mytable(serialno varchar(20),parameter varchar(20),value float)
insert into mytable select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
select @sql=@sql+', '+convert(char(10),value)+' as '+'['+parameter+']' from mytable where serialno='LJ832785QA'
set @sql='select ''LJ832785QA'' as serialno'+@sql
--select @sql
exec( @sql)
drop table mytable
怎么就结了????
insert into mytable select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
select @sql=@sql+', '+convert(char(10),value)+' as '+'['+parameter+']' from mytable where serialno='LJ832785QA'
set @sql='select ''LJ832785QA'' as serialno'+@sql
--select @sql
exec( @sql)
drop table mytable
怎么就结了????
#1
example:
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go
--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go
--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
--生成测试数据
create table 表(部门 int,人员 varchar(20))
insert into 表 select 1,'张三'
insert into 表 select 1,'李四'
insert into 表 select 1,'王五'
insert into 表 select 2,'赵六'
insert into 表 select 2,'邓七'
insert into 表 select 2,'刘八'
go
--创建用户定义函数
create function f_str(@department int)
returns varchar(8000)
as
begin
declare @ret varchar(8000)
set @ret = ''
select @ret = @ret+','+人员 from 表 where 部门 = @department
set @ret = stuff(@ret,1,1,'')
return @ret
end
go
--执行
select 部门,人员=dbo.f_str(部门) from 表 group by 部门 order by 部门
go
--输出结果
/*
部门 人员
---- --------------
1 张三,李四,王五
2 赵六,邓七,刘八
*/
--删除测试数据
drop function f_str
drop table 表
go
#2
libin_ftsafe(子陌红尘),谢谢你的回复,但我觉得你提供的跟我所要求的不是一个意思啊。
#3
--测试环境
create table #T(serialno varchar(20),parameter varchar(20),value float)
insert into #T select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
--动态SQL
declare @s varchar(2000)
select @s='select serialno '
select @s=@s+',['+parameter+']=sum(case when parameter='''+parameter+''' then value end) '
from #T
Group by parameter
set @s=@s+' From #T group by serialno'
exec(@s)
--结果
serialno Current Resistance Voltage
-------------------------------------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
--删除测试环境
Drop table #T
create table #T(serialno varchar(20),parameter varchar(20),value float)
insert into #T select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
--动态SQL
declare @s varchar(2000)
select @s='select serialno '
select @s=@s+',['+parameter+']=sum(case when parameter='''+parameter+''' then value end) '
from #T
Group by parameter
set @s=@s+' From #T group by serialno'
exec(@s)
--结果
serialno Current Resistance Voltage
-------------------------------------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
--删除测试环境
Drop table #T
#4
抱歉,看错了,以为是字符串求和,更正如下:
declare @s varchar(8000)
set @s = ''
select @s = @s+',['+parameter+']=sum(case parameter when '''+parameter+''' then value end)' from 表 group by parameter
set @s = 'select serialno'+@s+' from 表 group by serialno'
exec(@s)
declare @s varchar(8000)
set @s = ''
select @s = @s+',['+parameter+']=sum(case parameter when '''+parameter+''' then value end)' from 表 group by parameter
set @s = 'select serialno'+@s+' from 表 group by serialno'
exec(@s)
#5
可不可以用游标和表变量啊?
#6
如果parameter固定只有三个值,用静态SQL语句:
select
serialno,
Voltage = sum(case parameter when 'Voltage' then value end),
Current = sum(case parameter when 'Current' then value end),
Resistance = sum(case parameter when 'Resistance' then value end)
from
表
group by
serialno
select
serialno,
Voltage = sum(case parameter when 'Voltage' then value end),
Current = sum(case parameter when 'Current' then value end),
Resistance = sum(case parameter when 'Resistance' then value end)
from
表
group by
serialno
#7
create table A
(
serialno varchar(20),
parameter varchar(20),
value float
)
insert A
select 'LJ832785QA', 'Voltage', 0.2179481 union
select 'LJ832785QA', 'Current', 2.920481 union
select 'LJ832785QA', 'Resistance', 74.62748
--查询
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when parameter='''+parameter+''' then value else 0 end) as '''+parameter+''''
from A group by parameter
select @sql='select serialno'+@sql+' from A group by serialno'
exec(@sql)
--删除测试环境
drop table A
--结果
/*
serialno Current Resistance Voltage
-------------------- -----------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
*/
(
serialno varchar(20),
parameter varchar(20),
value float
)
insert A
select 'LJ832785QA', 'Voltage', 0.2179481 union
select 'LJ832785QA', 'Current', 2.920481 union
select 'LJ832785QA', 'Resistance', 74.62748
--查询
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+',max(case when parameter='''+parameter+''' then value else 0 end) as '''+parameter+''''
from A group by parameter
select @sql='select serialno'+@sql+' from A group by serialno'
exec(@sql)
--删除测试环境
drop table A
--结果
/*
serialno Current Resistance Voltage
-------------------- -----------------------------------------------------
LJ832785QA 2.9204810000000001 74.627480000000006 0.21794810000000001
*/
#8
create table mytable(serialno varchar(20),parameter varchar(20),value float)
insert into mytable select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
select @sql=@sql+', '+convert(char(10),value)+' as '+'['+parameter+']' from mytable where serialno='LJ832785QA'
set @sql='select ''LJ832785QA'' as serialno'+@sql
--select @sql
exec( @sql)
drop table mytable
怎么就结了????
insert into mytable select 'LJ832785QA','Voltage',0.2179481
union all select 'LJ832785QA','Current',2.920481
union all select 'LJ832785QA','Resistance',74.62748
select @sql=@sql+', '+convert(char(10),value)+' as '+'['+parameter+']' from mytable where serialno='LJ832785QA'
set @sql='select ''LJ832785QA'' as serialno'+@sql
--select @sql
exec( @sql)
drop table mytable
怎么就结了????