读书人

行转列解决方法

发布时间: 2012-01-23 21:57:28 作者: rapoo

行转列
数据
a,a1
a,a2
b,b1
b,b2
变成
a,a1,a2
b,b1,b2

[解决办法]
select a,min(col1) ,max(col2) from tb group by a
----

[解决办法]
估计不是楼主想要的
create table test ( col1 char(10),col2 char(10))
go
insert into test
select 'a ', 'a1 '
union all
select 'a ', 'a2 '
union all
select 'b ', 'b1 '
union all
select 'b ', 'b2 '
go
--select * from test

select col1,min(col2) ,max(col2) from test group by col1

drop table test

------
col1
---------- ---------- ----------
a a1 a2
b b1 b2

(所影响的行数为 2 行)


[解决办法]
create table test ( col1 char(10),col2 char(10))
go
insert into test
select 'a ', 'a1 '
union all
select 'a ', 'a2 '
union all
select 'b ', 'b1 '
union all
select 'b ', 'b2 '

create function test_f(@col1 varchar(2))
returns varchar(50)
as
begin
declare @s varchar(50)
select @s=isnull(@s+ ', ', ' ')+rtrim(col2) from test where col1=@col1
return @s
end

select distinct col1,col2=dbo.test_f(col1) from test

drop function test_f
drop table test
col1 col2
---------- --------------------------------------------------
a a1,a2
b b1,b2

(2 行受影响)


[解决办法]
create table test(col1 varchar(2),col2 varchar(2))
insert into test
select 'a ', 'a1 '
union all
select 'a ', 'a2 '
union all
select 'a ', 'a3 '
union all
select 'b ', 'b1 '
union all
select 'b ', 'b2 '

declare @sql varchar(8000),@c int,@ct1 int
select top 1 @ct1=count(col2) from test group by col1 order by count(col1) desc
set @sql= ' '
set @c=1
while @c <=@ct1
select @sql=@sql+
',(select col2 from test a where col1=b.col1 and (select count(1) from test where col1=b.col1 and col2 <=a.col2)= '+
cast(@c as varchar(2))+ ') [ '+cast(@c as varchar(2))+ '] ',@c=@c+1
exec( 'select col1 '+@sql+ ' from test b group by col1 ')

col1 1 2 3
---- ---- ---- ----
a a1 a2 a3
b b1 b2 NULL
[解决办法]
--假第一列列名A,第二列列名B,如果A相同的候,B不重
Create Table Test (A Varchar(10), B Varchar(10))

Insert Into TEST
Select 'a ', 'a1 '
Union All
Select 'a ', 'a2 '
Union All
Select 'b ', 'b1 '
Union All
Select 'b ', 'b2 '
GO
Declare @S Varchar(8000)
Select @S = 'Select A '
Select @S = @S + ', Max(Case OrderID When ' + Rtrim(OrderID) + ' Then B Else ' ' ' ' End) As B ' + Rtrim(OrderID)
From (Select *, OrderID =(Select Count(*) From TEST Where A = A.A And B <= A.B) From TEST A) B Group By OrderID
Select @S = @S + ' From (Select *, OrderID =(Select Count(*) From TEST Where A = A.A And B <= A.B) From TEST A) B Group By A '


EXEC(@S)
GO
Drop Table TEST
--Result
/*
AB1B2
aa1a2
bb1b2
*/
[解决办法]
--带符号合并行列转换

--有表t,其数据如下:
a b
1 1
1 2
1 3
2 1
2 2
3 1
--如何转换成如下结果:
a b
1 1,2,3
2 1,2
3 1

create table tb
(
a int,
b int
)
insert into tb(a,b) values(1,1)
insert into tb(a,b) values(1,2)
insert into tb(a,b) values(1,3)
insert into tb(a,b) values(2,1)
insert into tb(a,b) values(2,2)
insert into tb(a,b) values(3,1)
go

if object_id( 'pubs..f_hb ') is not null
drop function f_hb
go

--创建一个合并的函数
create function f_hb(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ' '
select @str = @str + ', ' + cast(b as varchar) from tb where a = @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct a ,dbo.f_hb(a) as b from tb

drop table tb

--结果
a b
----------- ------
1 1,2,3
2 1,2
3 1

(所影响的行数为 3 行)


多个前列的合并
数据的原始状态如下:
ID PR CON OP SC
001 p c 差 6
001 p c 好 2
001 p c 一般 4
002 w e 差 8
002 w e 好 7
002 w e 一般 1
===========================
用SQL语句实现,变成如下的数据
ID PR CON OPS
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)

if object_id( 'pubs..tb ') is not null
drop table tb
go

create table tb
(
id varchar(10),
pr varchar(10),
con varchar(10),
op varchar(10),
sc int
)

insert into tb(ID,PR,CON,OP,SC) values( '001 ', 'p ', 'c ', '差 ', 6)
insert into tb(ID,PR,CON,OP,SC) values( '001 ', 'p ', 'c ', '好 ', 2)
insert into tb(ID,PR,CON,OP,SC) values( '001 ', 'p ', 'c ', '一般 ', 4)
insert into tb(ID,PR,CON,OP,SC) values( '002 ', 'w ', 'e ', '差 ', 8)
insert into tb(ID,PR,CON,OP,SC) values( '002 ', 'w ', 'e ', '好 ', 7)
insert into tb(ID,PR,CON,OP,SC) values( '002 ', 'w ', 'e ', '一般 ', 1)
go

if object_id( 'pubs..test ') is not null
drop table test
go
select ID,PR,CON , OPS = op + '( ' + cast(sc as varchar(10)) + ') ' into test from tb

--创建一个合并的函数
if object_id( 'pubs..f_hb ') is not null
drop function f_hb
go
create function f_hb(@id varchar(10),@pr varchar(10),@con varchar(10))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ' '
select @str = @str + ', ' + cast(OPS as varchar) from test where id = @id and @pr = pr and @con = con
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct id ,pr , con , dbo.f_hb(id,pr,con) as OPS from test

drop table tb
drop table test

--结果
id pr con OPS
---------- ---------- ---------- -------------------
001 p c 差(6),好(2),一般(4)
002 w e 差(8),好(7),一般(1)

(所影响的行数为 2 行)



create table b
(col varchar(20))

insert b values ( 'a ')
insert b values ( 'b ')
insert b values ( 'c ')
insert b values ( 'd ')
insert b values ( 'e ')


declare @sql varchar(1024)
set @sql= ' '
select @sql=@sql+b.col+ ', ' from (select col from b) as b
set @sql= 'select ' ' '+@sql+ ' ' ' '
exec(@sql)

[解决办法]
create table test ( col1 char(10),col2 char(10))
go
insert into test
select 'a ', 'a1 '
union all
select 'a ', 'a2 '
union all
select 'b ', 'b1 '
union all
select 'b ', 'b2 '

--查询处理
DECLARE @s nvarchar(4000)
--交叉报表处理代码头
SET @s= 'SELECT col1 '
--生成列记录水平显示的处理代码拼接(处理Item列)
SELECT @s=@s
+ ', '+QUOTENAME(col2)
+N '=SUM(CASE col2 WHEN '+QUOTENAME(col2,N ' ' ' ')
+N ' THEN col2 END) '
FROM test
GROUP BY col2
--拼接交叉报表处理尾部,并且执行拼接后的动态SQL语句
EXEC(@s+N ' FROM test GROUP BY col1 ')
[解决办法]
create table test ( col1 char(10),col2 char(10))
go
insert into test
select 'a ', 'a1 '
union all
select 'a ', 'a2 '
union all
select 'b ', 'b1 '
union all
select 'b ', 'b2 '

--查询处理
DECLARE @s nvarchar(4000)
--交叉报表处理代码头
SET @s= 'SELECT col1 '
--生成列记录水平显示的处理代码拼接(处理Item列)
SELECT @s=@s
+ ', '+QUOTENAME(col2)
+N '=max(CASE col2 WHEN '+QUOTENAME(col2,N ' ' ' ')
+N ' THEN col2 END) '
FROM test
GROUP BY col2
--拼接交叉报表处理尾部,并且执行拼接后的动态SQL语句
EXEC(@s+N ' FROM test GROUP BY col1 ')

读书人网 >SQL Server

热点推荐