合併有資料的列

2021-09-07 08:01:12 字數 3278 閱讀 8249

--示例資料:

create table t1(

name varchar(20),

col1 varchar(2),

col2 varchar(2),

col3 varchar(2))

insert t1(name,col1,col2,col3)

select 'a','1','','' union all

select 'a','2','','' union all

select 'a','3','','' union all

select 'a','','4','' union all

select 'a','','5','' union all

select 'a','','6','' union all

select 'a','','7','' union all

select 'a','','','8' union all

select 'a','','','9' union all

select 'a','','','0' union all

select 'b','11','','' union all

select 'b','12','','' union all

select 'b','13','','' union all

select 'b','14','','' union all

select 'b','','15','' union all

select 'b','','16','' union all

select 'b','','17','' union all

select 'b','','','18' union all

select 'b','','','19' union all

select 'b','','','10' 

go--問題描述:

表t1中的列數不定,固定有乙個name列,大於1個的coln列,列名為col1~coln,要求根據name分組,合併各coln列中有值的資料。對於示例資料,要求結果如下:

name    col1    col2    col3 

---------- --------- --------- -------- 

a            1          4           0

a            2          5           8

a            3          6           9

a                        7    

b           11        15        10

b           12        16        18

b           13        17        19

b           14        

--鄒建的sql2000下解決辦法:

select name=isnull(c.name,isnull(b.name,a.name)),

col1=isnull(a.col1,''),         

[col2]=isnull(b.[col2],''),         

[col3]=isnull(c.[col3],'')     

from(select name,

col1,

id=(select count(*) from t1 where name=a.name and col1>'' and col1<=a.col1)

from t1 a where col1>'' )a

full join

(select name,

[col2],

id=(select count(*) from t1 where name=a.name and [col2]>'' and [col2]<=a.[col2])

from t1 a where [col2]>'')b

on a.name=b.name and a.id=b.id

full join

(select name,

[col3],

id=(select count(*) from t1 where name=a.name and [col3]>'' and [col3]<=a.[col3])

from t1 a where [col3]>'')c on a.name=c.name and a.id=c.id

--我在sql2005下寫的方法(這裡用到了cte):

with tt1 as

(select row_number() over (partition by name order by col1) as t,

name,

col1

from t1 where col1 <> ''

),tt2 as

(select row_number() over (partition by name order by col2) as t,

name,

col2

from t1 where col2 <> ''

),tt3 as

(select row_number() over (partition by name order by col3) as t,

name,

col3

from t1 where col3 <> ''

)select

case when tt1.name is not null then tt1.name

when tt2.name is not null then tt2.name

when tt3.name is not null then tt3.name

end as name,

case when tt1.col1 is null then ''

else tt1.col1

end as col1,

case when tt2.col2 is null then ''

else tt2.col2

end as col2,

case when tt3.col3 is null then ''

else tt3.col3

end as col3

from tt1 full join tt2 on tt1.t = tt2.t and tt1.name = tt2.name

full join tt3 on tt1.t = tt3.t and tt1.name=tt3.name

合併有資料的列

bom資料排序及分級顯示 示例資料 create table t1 name varchar 20 col1 varchar 2 col2 varchar 2 col3 varchar 2 insert t1 name,col1,col2,col3 select a 1 union all sele...

合併有資料的列

bom資料排序及分級顯示 示例資料 create table t1 name varchar 20 col1 varchar 2 col2 varchar 2 col3 varchar 2 insert t1 name,col1,col2,col3 select a 1 union all sele...

合併有資料的列

bom資料排序及分級顯示 示例資料 create table t1 name varchar 20 col1 varchar 2 col2 varchar 2 col3 varchar 2 insert t1 name,col1,col2,col3 select a 1 union all sele...