22,207
社区成员
发帖
与我相关
我的任务
分享
你表有ID,2000的也可以直接查询:
declare @sql varchar(8000),@s varchar(1000)
select @s=isnull(@s+',m.','m.')+name from syscolumns where object_id('tb1')=id
set @sql = 'select '+@s
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then n.Name else '''' end) [WageName' + cast(px as varchar) + ']'
+ ' , max(case px when ''' + cast(px as varchar) + ''' then cast(n.Wage as varchar) else '''' end) [Wage' + cast(px as varchar) + ']'
from (select distinct px from (select t.* , px = (select COUNT(1) from tb2 where WageID =t.WageID and ID<=t.ID) from tb2 t) k) as a
set @sql = @sql + ' from tb1 m , (select t.* , px = (select COUNT(1) from tb2 where WageID =t.WageID and ID<=t.ID) from tb2 t) n where m.WageID = n.WageID group by '+@s
exec(@sql)
/*
ID Name WageID WageName1 Wage1 WageName2 Wage2 WageName3 Wage3
----------- ---------- ----------- ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------
1 zy 1 补助 100 保险 500 奖金 200
2 zc 2 费用1 50 费用2 300
(2 行受影响)
*/
create table tb1(ID int,Name nvarchar(10), WageID int)
insert into tb1 values(1, N'zy', 1)
insert into tb1 values(2, N'zc', 2)
create table tb2(ID int,WageID int,Name nvarchar(10),Wage int)
insert into tb2 values(1 ,1 ,N'补助' ,100)
insert into tb2 values(2 ,1 ,N'保险' ,500)
insert into tb2 values(3 ,1 ,N'奖金' ,200)
insert into tb2 values(4 ,2 ,N'费用1', 50)
insert into tb2 values(5 ,2 ,N'费用2', 300)
go
declare @sql varchar(8000),@s varchar(1000)
select @s=isnull(@s+',m.','m.')+name from syscolumns where object_id('tb1')=id
set @sql = 'select '+@s
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then n.Name else '''' end) [WageName' + cast(px as varchar) + ']'
+ ' , max(case px when ''' + cast(px as varchar) + ''' then cast(n.Wage as varchar) else '''' end) [Wage' + cast(px as varchar) + ']'
from (select distinct px from (select t.* , px = row_number() over(partition by WageID order by id) from tb2 t) k) as a
set @sql = @sql + ' from tb1 m , (select t.* , px = row_number() over(partition by WageID order by id) from tb2 t) n where m.WageID = n.WageID group by '+@s
exec(@sql)
/*
ID Name WageID WageName1 Wage1 WageName2 Wage2 WageName3 Wage3
----------- ---------- ----------- ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------
1 zy 1 补助 100 保险 500 奖金 200
2 zc 2 费用1 50 费用2 300
(2 行受影响)
*/
drop table tb1 , tb2
declare @s varchar(1000)
select @s=isnull(@s+',','')+name from syscolumns where object_id('表1名称')=id
print @s
create table tb1(ID int,Name nvarchar(10), WageID int)
insert into tb1 values(1, N'zy', 1)
insert into tb1 values(2, N'zc', 2)
create table tb2(ID int,WageID int,Name nvarchar(10),Wage int)
insert into tb2 values(1 ,1 ,N'补助' ,100)
insert into tb2 values(2 ,1 ,N'保险' ,500)
insert into tb2 values(3 ,1 ,N'奖金' ,200)
insert into tb2 values(4 ,2 ,N'费用1', 50)
insert into tb2 values(5 ,2 ,N'费用2', 300)
go
declare @sql varchar(8000)
set @sql = 'select m.ID , m.Name , m.WageID '
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then n.Name else '''' end) [WageName' + cast(px as varchar) + ']'
+ ' , max(case px when ''' + cast(px as varchar) + ''' then cast(n.Wage as varchar) else '''' end) [Wage' + cast(px as varchar) + ']'
from (select distinct px from (select t.* , px = row_number() over(partition by WageID order by id) from tb2 t) k) as a
set @sql = @sql + ' from tb1 m , (select t.* , px = row_number() over(partition by WageID order by id) from tb2 t) n where m.WageID = n.WageID group by m.ID , m.Name , m.WageID'
exec(@sql)
drop table tb1 , tb2
/*
ID Name WageID WageName1 Wage1 WageName2 Wage2 WageName3 Wage3
----------- ---------- ----------- ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------
1 zy 1 补助 100 保险 500 奖金 200
2 zc 2 费用1 50 费用2 300
(2 行受影响)
*/
create table tb1(ID int,Name varchar(10), WageID int)
insert into tb1 values(1, 'zy', 1)
insert into tb1 values(2, 'zc', 2)
create table tb2(ID int,WageID int,Name varchar(10),Wage int)
insert into tb2 values(1 ,1 ,'补助' ,100)
insert into tb2 values(2 ,1 ,'保险' ,500)
insert into tb2 values(3 ,1 ,'奖金' ,200)
insert into tb2 values(4 ,2 ,'费用1', 50)
insert into tb2 values(5 ,2 ,'费用2', 300)
go
declare @sql varchar(8000)
set @sql = 'select m.ID , m.Name , m.WageID '
select @sql = @sql + ' , max(case px when ''' + cast(px as varchar) + ''' then n.Name else '''' end) [WageName' + cast(px as varchar) + ']'
+ ' , max(case px when ''' + cast(px as varchar) + ''' then cast(n.Wage as varchar) else '''' end) [Wage' + cast(px as varchar) + ']'
from (select distinct px from (select t.* , px = (select count(1) from tb2 where WageID = t.WageID and id < t.id) + 1 from tb2 t) k) as a
set @sql = @sql + ' from tb1 m , (select t.* , px = (select count(1) from tb2 where WageID = t.WageID and id < t.id) + 1 from tb2 t) n where m.WageID = n.WageID group by m.ID , m.Name , m.WageID'
exec(@sql)
drop table tb1 , tb2
/*
ID Name WageID WageName1 Wage1 WageName2 Wage2 WageName3 Wage3
----------- ---------- ----------- ---------- ------------------------------ ---------- ------------------------------ ---------- ------------------------------
1 zy 1 补助 100 保险 500 奖金 200
2 zc 2 费用1 50 费用2 300
*/
--静态:
select tb1.id,
tb1.name,
tb1.wageid,
wagename1=MAX(case when rn=1 then tb2.name end),
wage1=MAX(case when rn=1 then tb2.wage end),
wagename2=MAX(case when rn=2 then tb2.name end),
wage2=MAX(case when rn=2 then tb2.wage end),
wagename3=MAX(case when rn=3 then tb2.name end),
wage3=MAX(case when rn=3 then tb2.wage end)
from tb1 join
(select *,rn=ROW_NUMBER()over(PARTITION by wageid order by id) from tb2 ) tb2
on tb1.wageid=tb2.wageid
group by tb1.id,
tb1.name,
tb1.wageid