Tbl 1
ID Name Email
1 A a@b.c
2 B b@c.d
Tbl2
ID Related_ID Value Name
1 1 Z Name1
2 1 Y Name2
3 2 X Name1
4 2 W Name2
5 2 G Name3
我可以写一个查询来显示
ID Name Email Value1
1 A a@b.c Z
1 A a@b.c Y
我怎么写它才会变得像
ID Name Email Value1 Value2
1 A a@b.c Z Y
2 B b@c.d X W
答案 0 :(得分:0)
可以使用PIVOT声明来完成 以下是您可以查看的几个链接 http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/
答案 1 :(得分:0)
以下是使用动态sql获取所需内容的示例。
请注意,CTE测试和临时表#tmp2应映射到原始表。
create table #tmp (ID int, Related_ID int, Value nvarchar(50), Name nvarchar(50), rn int)
delete #tmp
create table #tmp2 (ID int, Name nvarchar(50), Email nvarchar(50))
delete #tmp2
;with test(ID, Related_ID, Value, Name)
as
(
select 1, 1, 'Z', 'Name1'
union all
select 2, 1, 'Y', 'Name2'
union all
select 3, 2, 'X', 'Name1'
union all
select 4, 2, 'W', 'Name2'
union all
select 5, 2, 'G', 'Name3'
)
insert into #tmp
select *, row_number() OVER (partition by Related_ID order by ID) as rn
from test
insert into #tmp2
select 1, 'A', 'a@b.c'
union all
select 2, 'B', 'b@c.d'
declare @d nvarchar(MAX)
,@e nvarchar(MAX)
SET @d = 'select a.ID, a.Name, a.Email '
SET @e = ',min(case b.rn when >rn< then Value else null end) as [Value>rn<]'
select @d = @d + (
select t from(
select distinct replace(@e,'>rn<', b.rn) as [text()]
from #tmp b
for xml path('')) c(t)
)
set @d = @d + '
from #tmp2 a
join #tmp b
on a.ID = b.Related_ID
group by a.ID, a.Name, a.Email'
exec sp_executesql @d
drop table #tmp
drop table #tmp2
答案 2 :(得分:0)
declare @t1 table(id int, name varchar(20), email varchar(32))
declare @t2 table(id int, related_id int, value varchar(10), name varchar(10))
insert @t1 values(1,'A', 'a@b.c')
insert @t1 values(2,'B', 'b@c.d')
insert @t2 values(1, 1, 'Z', 'Name1')
insert @t2 values(2, 1, 'Y', 'Name2')
insert @t2 values(3, 2, 'X', 'Name1')
insert @t2 values(4, 2, 'W', 'Name2')
insert @t2 values(5, 2, 'G', 'Name3')
;with a as
(
select value, related_id, ROW_NUMBER() over(PARTITION BY related_id order by value desc) rn
from @t2
), b as
(
select value, related_id, rn from a where rn in (1,2)
)
select t5.ID, t5.Name, t5.Email, t3.value Value1, t4.value Value2
from @t1 t5
join b t3
on t3.related_id = t5.id and t3.rn = 1
join b t4
on t4.related_id = t5.id and t4.rn = 2
结果:
ID Name Email Value1 Value2
------ ------ ---------- -------- ----------
1 A a@b.c Z Y
2 B b@c.d X W