我有这张桌子
job id op id hours_actu hours_est hours_plan work_id
10195 31 0 3.12 3.12 2011
10195 30 2.25 0.67 0.67 3042
10195 31 4.5 3.34 3.34 3042
10195 15 2.25 0.67 0.67 3042
10195 16 4.5 3.34 3.34 3042
我需要进行查询才能获得这样的数据
job id 30,31_actu 30,31_est 30,31_plan 15,16_actu 15,16_est 15,16_plan work_id
10195 0 3.12 3.12 2011
10195 6.75 4.01 4.01 6.75 4.01 4.01 3042
答案 0 :(得分:0)
我不确定如何动态更改列名,但我有一个想法,就像keepint一样,我试过这样的常量列名。
select
x.[job id],
sum(x.[30,31,32_actu]) [30,31,32_actu],sum(x.[30,31,32_est]) [30,31,32_est],sum(x.[30,31,32_plan]) [30,31,32_plan],
sum(x.[15,16,17_actu]) [15,16,17_actu],sum(x.[15,16,17_est]) [15,16,17_est],sum(x.[15,16,17_plan]) [15,16,17_plan],
x.work_id
from
(
select
a.[job id],a.[op id],
[30,31,32_actu] = (case when a.[op id] between 30 and 32 then a.hours_actu else 0 end),
[30,31,32_est] = (case when a.[op id] between 30 and 32 then a.hours_est else 0 end),
[30,31,32_plan] = (case when a.[op id] between 30 and 32 then a.hours_plan else 0 end),
[15,16,17_actu] = (case when a.[op id] between 15 and 17 then a.hours_actu else 0 end),
[15,16,17_est] = (case when a.[op id] between 15 and 17 then a.hours_est else 0 end),
[15,16,17_plan] = (case when a.[op id] between 15 and 17 then a.hours_plan else 0 end),
a.work_id
from table1 a
) x
group by x.work_id,x.[job id]
答案 1 :(得分:0)
如果您想动态命名列,请尝试此操作.............................
select * into #table1 from
(select 10195 [job id],31 [op id],0 [hours_actu],3.12 [hours_est] ,3.12 hours_plan ,2011 work_id
union all
select 10195 ,30 ,2.25 ,0.67 ,0.67 ,3042
union all
select 10195 ,31 ,4.5 ,3.34 ,3.34 ,3042
union all
select 10195 ,15 ,2.25 ,0.67 ,0.67 ,3042
union all
select 10195 ,16 ,4.5 ,3.34 ,3.34 ,3042) x
declare @t table (col1 int,col2 int)
insert into @t
select y.Col1,z.Col2 from (
select row_number() over (order by x.Col1) rn, x.Col1 from (select distinct b.[op id] Col1 from #table1 b where b.[op id] between 30 and 32) x) y
full outer join
(select row_number() over (order by x.Col2) rn, x.Col2 from (select distinct b.[op id] Col2 from #table1 b where b.[op id] between 15 and 17) x) z
on y.rn = z.rn
declare @tab nvarchar(max),@col1 varchar(100),@col2 varchar(100)
set @col1 = substring((select ',' + convert(varchar(10), x.col1) from @t x for xml path('')),2,100)
set @col2 = substring((select ',' + convert(varchar(10), x.col2) from @t x for xml path('')),2,100)
set @tab = 'create table #t ( [job id] int, [' + @col1 + '_actu] money, [' + @col1 + '_est] money, [' + @col1 + '_Plan] money, [' + @col2 + '_actu] money, [' + @col2 + '_est] money, [' + @col2 + '_Plan] money, [work_id] int) '
set @tab = @tab + 'insert into #t
select
x.[job id],
sum(x.[30,31,32_actu]) [30,31,32_actu],sum(x.[30,31,32_est]) [30,31,32_est],sum(x.[30,31,32_plan]) [30,31,32_plan],
sum(x.[15,16,17_actu]) [15,16,17_actu],sum(x.[15,16,17_est]) [15,16,17_est],sum(x.[15,16,17_plan]) [15,16,17_plan],
x.work_id
from
(
select
a.[job id],a.[op id],
[30,31,32_actu] = (case when a.[op id] between 30 and 32 then a.hours_actu else 0 end),
[30,31,32_est] = (case when a.[op id] between 30 and 32 then a.hours_est else 0 end),
[30,31,32_plan] = (case when a.[op id] between 30 and 32 then a.hours_plan else 0 end),
[15,16,17_actu] = (case when a.[op id] between 15 and 17 then a.hours_actu else 0 end),
[15,16,17_est] = (case when a.[op id] between 15 and 17 then a.hours_est else 0 end),
[15,16,17_plan] = (case when a.[op id] between 15 and 17 then a.hours_plan else 0 end),
a.work_id
from #table1 a
) x
group by x.work_id,x.[job id]'
set @tab = @tab + 'select * from #t'
EXEC sys.sp_executesql @tab
drop table #table1