您的位置:程序门 -> ms-sql server -> 基础类



求多行记录转换成单行记录sql语句。


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求多行记录转换成单行记录sql语句。
发表于:2007-07-02 14:22:29 楼主
我有一个table,temptable。
cola             colb             colc
qq1001         1                   yuuu
qq1001         2                   ttgg
qq1001         3                   ffgosdfa
qq1002         1                   ewt
qq1002         2                   dsfkds

得到的结果是,
qq1001             yuuu,ttgg,ffgosdfa
qq1002             ewt,dsfkds

用一条sql语句可以做到吗?帮忙写一下
发表于:2007-07-02 14:26:341楼 得分:0
--創建函數
create   function   f_test(@cola   varchar(10))
returns   varchar(4000)
as
begin
declare   @s   varchar(4000)
select   @s   =   ' '
select   @s   =   @s   +   ', '   +   colc   from   temptable   where   cola   =   @cola
select   @s   =   stuff(@s,   1,   1,   ' ')
return   @s
end
go
--測試
select
cola,
dbo.f_test(cola)   as   colc
from
temptable
group   by
cola
发表于:2007-07-02 14:26:572楼 得分:0
--建立測試環境
create   table   temptable
(cola   varchar(10),
  colb   int,
  colc   varchar(10))
insert   temptable   select   'qq1001 ',   1,               n 'yuuu '
union   all   select   'qq1001 ',   2,               n 'ttgg '
union   all   select   'qq1001 ',   3,               n 'ffgosdfa '
union   all   select   'qq1002 ',   1,               n 'ewt '
union   all   select   'qq1002 ',   2,               n 'dsfkds '
go
--創建函數
create   function   f_test(@cola   varchar(10))
returns   varchar(4000)
as
begin
declare   @s   varchar(4000)
select   @s   =   ' '
select   @s   =   @s   +   ', '   +   colc   from   temptable   where   cola   =   @cola   order   by   colb
select   @s   =   stuff(@s,   1,   1,   ' ')
return   @s
end
go
--測試
select
cola,
dbo.f_test(cola)   as   colc
from
temptable
group   by
cola
go
--刪除測試環境
drop   table   temptable
drop   function   f_test
--結果
/*
cola colc
qq1001 yuuu,ttgg,ffgosdfa
qq1002 ewt,dsfkds
*/
发表于:2007-07-02 14:38:133楼 得分:0
厉害!!
发表于:2007-07-02 14:40:444楼 得分:0
create   table   temptable

cola           varchar(10),
  colb     varchar(10),
        colc   varchar(10)
)
delete   temptable  
insert   into   temptable

select

'qq1001 '   ,       '1 '         ,         '   yuuu '   union   all   select  
'qq1001 '     ,     '2 '     ,               'ttgg '   union   all   select  
'qq1001 '   ,       '3 '       ,           'ffgosdfa '   union   all   select  
'qq1002 '     ,   '   1 '         ,           'ewt '   union   all   select  
'qq1002 '   ,     '   2 '     ,           'dsfkds 'union   all   select  
'qq1003 '     ,   '   1 '         ,           'ewt '   union   all   select  
'qq1003 '   ,     '   2 '     ,           'dsfkds '

select   *   from   temptable


alter   function   temptable_function(@cola   varchar(10))
returns   varchar(4000)
as
begin
declare   @s   varchar(4000)
select   @s   =   ' '
select   @s   =   @s   +   ', '   +   colc   from   temptable   where   cola   =   @cola
select   @s   =   substring(@s,   2,   len(@s))
return   @s
end


select   cola,   dbo.temptable_function(cola)   from   temptable   group   by   cola

---------------------------------------
qq1001   yuuu,ttgg,ffgosdfa
qq1002 ewt,dsfkds
qq1003 ewt,dsfkds
发表于:2007-07-02 14:41:285楼 得分:0
alter     --------------->   create
发表于:2007-07-02 15:04:486楼 得分:0
太厉害了!
发表于:2007-07-03 17:48:137楼 得分:0
如果我colc有些有空记录,这样就会出现有两个逗号,或者全部空记录,select出来的就只剩下逗号,怎么解决呢?
发表于:2007-07-03 17:51:158楼 得分:0
加上判斷

--建立測試環境
create   table   temptable
(cola   varchar(10),
  colb   int,
  colc   varchar(10))
insert   temptable   select   'qq1001 ',   1,               n 'yuuu '
union   all   select   'qq1001 ',   2,               n 'ttgg '
union   all   select   'qq1001 ',   3,               n 'ffgosdfa '
union   all   select   'qq1002 ',   1,               n 'ewt '
union   all   select   'qq1002 ',   2,               n 'dsfkds '
union   all   select   'qq1002 ',   3,               n ' '
union   all   select   'qq1002 ',   4,               null
union   all   select   'qq1002 ',   5,               n 'dasdsa '
union   all   select   'qq1003 ',   1,               n ' '
go
--創建函數
create   function   f_test(@cola   varchar(10))
returns   varchar(4000)
as
begin
declare   @s   varchar(4000)
select   @s   =   ' '
select   @s   =   @s   +   ', '   +   colc   from   temptable   where   cola   =   @cola   and   isnull(colc,   ' ')   !=   ' '   order   by   colb
select   @s   =   stuff(@s,   1,   1,   ' ')
return   @s
end
go
--測試
select
cola,
dbo.f_test(cola)   as   colc
from
temptable
group   by
cola
go
--刪除測試環境
drop   table   temptable
drop   function   f_test
--結果
/*
cola colc
qq1001 yuuu,ttgg,ffgosdfa
qq1002 ewt,dsfkds,dasdsa
qq1003 null
*/




快速检索

最新资讯
热门点击