表数据如下:
房间号 单元
101 东单元
201 东单元
101 中单元
201 中单元
101 西单元
201 西单元
301 西单元
用sql语句转换成: 房间 单元 房间 单元 房间 单元
301 西单元
201 东单元 201 中单元 201 西单元
101 东单元 101 中单元 101 西单元
房间号 单元
101 东单元
201 东单元
101 中单元
201 中单元
101 西单元
201 西单元
301 西单元
用sql语句转换成: 房间 单元 房间 单元 房间 单元
301 西单元
201 东单元 201 中单元 201 西单元
101 东单元 101 中单元 101 西单元
where 单元 like '东%'
union
select '' as 房间号,'' as 单元,'' as 楼号,房间号1,单元1, 楼号1,'' as 房间号2,'' as 单元2,'' as 楼号2 from 表
where 单元 like '中%'
select '' as 房间号,'' as 单元,'' as 楼号,房间号1,单元1, 楼号1, 房间号2,单元2,楼号2 from 表
where 单元 like '西%'
order by 房间号
where 单元 like '东%'
union all
select '' as 房间号,'' as 单元,'' as 楼号,房间号1,单元1, 楼号1,'' as 房间号2,'' as 单元2,'' as 楼号2 from 表
where 单元 like '中%'
union all
select '' as 房间号,'' as 单元,'' as 楼号,房间号1,单元1, 楼号1, 房间号2,单元2,楼号2 from 表
where 单元 like '西%'
order by 房间号
房间=max(case 单元 when '东单元' then rtrim(房间号) else '空' end),
单元=max(case 单元 when '东单元' then 单元 else '空' end),
房间=max(case 单元 when '中单元' then rtrim(房间号) else '空' end),
单元=max(case 单元 when '中单元' then 单元 else '空' end),
房间=max(case 单元 when '西单元' then rtrim(房间号) else '空' end),
单元=max(case 单元 when '西单元' then 单元 else '空' end)
from
表
group by
房间号
insert into # select '101','东单元'
insert into # select '201','东单元'
insert into # select '101','中单元'
insert into # select '201','中单元'
insert into # select '101','西单元'
insert into # select '201','西单元'
insert into # select '301','西单元'
select
房间=max(case 单元 when '东单元' then rtrim(房间号) end),
单元=max(case 单元 when '东单元' then 单元 end),
房间=max(case 单元 when '中单元' then rtrim(房间号) end),
单元=max(case 单元 when '中单元' then 单元 end),
房间=max(case 单元 when '西单元' then rtrim(房间号) end),
单元=max(case 单元 when '西单元' then 单元 end)
from
#
group by
房间号
/*
房间 单元 房间 单元 房间 单元
------------ ---------- ------------ ---------- ------------ ----------
101 东单元 101 中单元 101 西单元
201 东单元 201 中单元 201 西单元
NULL NULL NULL NULL 301 西单元
*/
declare @s varchar(8000)
set @s=''
select @s=@s+',房间=max(case 单元 when '''+单元+''' then rtrim(房间号) end)'
+',单元=max(case 单元 when '''+单元+''' then 单元 end)'
from # group by 单元
set @s='select '+stuff(@s,1,1,'')+' from # group by 房间号'
exec(@s)/*
房间 单元 房间 单元 房间 单元
------------ ---------- ------------ ---------- ------------ ----------
101 东单元 101 西单元 101 中单元
201 东单元 201 西单元 201 中单元
NULL NULL 301 西单元 NULL NULL
*/drop table #
(select * from 表 where 单元='东单元')a
full join (select * from 表 where 单元='中单元') b on a.房间号=b.房间号
full join (select * from 表 where 单元='西单元') c on a.房间号=c.房间号)
order by c.房间号 desc
insert into # select '101','东单元'
insert into # select '201','东单元'
insert into # select '101','中单元'
insert into # select '201','中单元'
insert into # select '101','西单元'
insert into # select '201','西单元'
insert into # select '301','西单元' select
房间=max(case 单元 when '东单元' then rtrim(房间号) else ' 空' end),
单元=max(case 单元 when '东单元' then 单元 else ' 空' end),
房间=max(case 单元 when '中单元' then rtrim(房间号) else ' 空' end),
单元=max(case 单元 when '中单元' then 单元 else ' 空' end),
房间=max(case 单元 when '西单元' then rtrim(房间号) else ' 空' end),
单元=max(case 单元 when '西单元' then 单元 else ' 空' end)
from
#
group by
房间号
insert 表 select '101','东单元','1'
union all select '201','东单元','1'
union all select '101','中单元','1'
union all select '201','中单元','1'
union all select '101','西单元','1'
union all select '201','西单元','1'
union all select '301','西单元','1'