日期:2014-05-16  浏览次数:20413 次

SQLServer 把表的挨着的四条数据合并到一起

------解决方案--------------------
看图应该是mssql2005+版本,
那你可以用 row_number提成好序,

把 row_number/4 的分别 =0 =1 =2 =3 连起来就行了。
------解决方案--------------------
select
     max(case when (id-1)%4=0 then name else '' end) ,
     max(case when (id-1)%4=0 then pass else '' end) ,
     max(case when (id-1)%4=1 then name else '' end) ,
     max(case when (id-1)%4=1 then pass else '' end) ,
     max(case when (id-1)%4=2 then name else '' end) ,
     max(case when (id-1)%4=2 then pass else '' end) ,
     max(case when (id-1)%4=3 then name else '' end) ,
     max(case when (id-1)%4=3 then pass else '' end) 
from
    (select id=row_number()over(order by getdate()),* from tb) as t


------解决方案--------------------
这个得用动态语句来实现比较好
------解决方案--------------------
引用:
a          a         
b          b         
c          c         
d          d         
e          e         
f          f         
g          g         
h          h         
i              i  
我的测试数据,执行你的语句的效果 


试试这个:

--drop table t

create table t(name varchar(10),pass varchar(10))

insert into t
select 'a',          'a' union all         
select 'b',          'b' union all         
 select 'c',          'c' union all         
 select 'd',          'd' union all         
 select 'e',          'e' union all         
 select 'f',          'f' union all