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 col1drop 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 endselect distinct col1,col2=dbo.test_f(col1) from testdrop function test_f drop table test col1 col2 ---------- -------------------------------------------------- a a1,a2 b b1,b2(2 行受影响)
2000或以下要用函数,2005可以不用 --新增一条 insert into test select 'a','a3'select distinct col1,col2=dbo.test_f(col1) from test col1 col2 ---------- -------------------------------------------------- a a1,a2,a3 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 /* A B1 B2 a a1 a2 b b1 b2 */
--假設第一列列名為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 'a','a3' Union All Select 'b','b1' Union All Select 'b','b2' GO Declare @S Varchar(8000)Select ID = Identity(Int, 1, 1), * Into #T From TEST 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 #T Where A = A.A And ID <= A.ID) From #T A) B Group By OrderID Select @S = @S + ' From (Select *, OrderID =(Select Count(*) From #T Where A = A.A And ID <= A.ID) From #T A) B Group By A' EXEC(@S)Drop Table #T GO Drop Table TEST --Result /* A B1 B2 B3 a a1 a2 a3 b b1 b2 */
--带符号合并行列转换--有表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) goif 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 tbdrop 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 gocreate 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) goif 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 testdrop 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')
----
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 col1drop table test------
col1
---------- ---------- ----------
a a1 a2
b b1 b2 (所影响的行数为 2 行)
数据
a,a1
a,a2
a,a3
b,b1
b,b2
变成
a,a1,a2,a3
b,b1,b2
也可以
(
a,a1,a2,a3
b,b2,b1
顺序没关系)
怎么做呢
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
endselect distinct col1,col2=dbo.test_f(col1) from testdrop function test_f
drop table test
col1 col2
---------- --------------------------------------------------
a a1,a2
b b1,b2(2 行受影响)
--新增一条
insert into test
select 'a','a3'select distinct col1,col2=dbo.test_f(col1) from test
col1 col2
---------- --------------------------------------------------
a a1,a2,a3
b b1,b2(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
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
/*
A B1 B2
a a1 a2
b b1 b2
*/
Create Table Test (A Varchar(10), B Varchar(10))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'
GO
Declare @S Varchar(8000)Select ID = Identity(Int, 1, 1), * Into #T From TEST
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 #T Where A = A.A And ID <= A.ID) From #T A) B Group By OrderID
Select @S = @S + ' From (Select *, OrderID =(Select Count(*) From #T Where A = A.A And ID <= A.ID) From #T A) B Group By A'
EXEC(@S)Drop Table #T
GO
Drop Table TEST
--Result
/*
A B1 B2 B3
a a1 a2 a3
b b1 b2
*/
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)
goif 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 tbdrop 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
gocreate 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)
goif 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 testdrop 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)
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')
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')