SQL Server(2012):多列上的透视(或分组依据?)
我有下表:SQL Server(2012):多列上的透视(或分组依据?),sql,sql-server,Sql,Sql Server,我有下表: month seating_id dept_id Jan 1 5 Jan 8 9 Jan 5 3 Jan 7 2 Jan 1 5 Feb 1 9 Feb 8 9 Feb 5 3 Feb 7 2 Feb 7
month seating_id dept_id
Jan 1 5
Jan 8 9
Jan 5 3
Jan 7 2
Jan 1 5
Feb 1 9
Feb 8 9
Feb 5 3
Feb 7 2
Feb 7 1
我想计算每个月的座位id
和部门id
的每种类型,因此结果如下所示:
month seating_id_1 seating_id_5 seating_id_7 seating_id_8 dept_id_1 dept_id_2 dept_id_3 dept_id_5 dept_id_9
Jan 2 1 1 1 0 1 1 2 1
Feb 0 1 2 1 1 1 1 0 2
我曾经尝试过,但是没有达到预期的效果。注意,如果可能的话,我希望将其作为SELECT语句执行,而不是过程调用
如果您需要任何其他信息,请告诉我。我有一个答案,您可能不喜欢,但它确实做到了:
select month
, sum(case seating_id when 1 then 1 else 0 end) as seating_id_1
, sum(case seating_id when 2 then 1 else 0 end) as seating_id_2
...
, sum(case dept_id when 1 then 1 else 0 end) as dept_id_1
, sum(case dept_id when 2 then 1 else 0 end) as dept_id_2
...
from YourTable
group by month
我有一个你可能不喜欢的答案,但确实如此:
select month
, sum(case seating_id when 1 then 1 else 0 end) as seating_id_1
, sum(case seating_id when 2 then 1 else 0 end) as seating_id_2
...
, sum(case dept_id when 1 then 1 else 0 end) as dept_id_1
, sum(case dept_id when 2 then 1 else 0 end) as dept_id_2
...
from YourTable
group by month
以防你需要动起来 示例
Declare @SQL varchar(max) = '
Select *
From (
Select month,B.*
From YourTable A
Cross Apply (values (concat(''seating_id_'',seating_id),seating_id)
,(concat(''dept_id_'',dept_id),dept_id)
) b(item,value)
) A
Pivot (count([Value]) For [Item] in (' + Stuff((Select Distinct concat(',[seating_id_',seating_id,']') from YourTable For XML Path('')),1,1,'')
+','+
Stuff((Select Distinct concat(',[dept_id_',dept_id,']') from YourTable For XML Path('')),1,1,'')
+ ') ) p'
Exec(@SQL);
返回
生成的SQL如下所示
Select *
From (
Select month,B.*
From YourTable A
Cross Apply (values (concat('seating_id_',seating_id),seating_id)
,(concat('dept_id_',dept_id),dept_id)
) b(item,value)
) A
Pivot (count([Value]) For [Item] in ([seating_id_1],[seating_id_5],[seating_id_7],[seating_id_8],[dept_id_1],[dept_id_2],[dept_id_3],[dept_id_5],[dept_id_9]) ) p
以防你需要动起来 示例
Declare @SQL varchar(max) = '
Select *
From (
Select month,B.*
From YourTable A
Cross Apply (values (concat(''seating_id_'',seating_id),seating_id)
,(concat(''dept_id_'',dept_id),dept_id)
) b(item,value)
) A
Pivot (count([Value]) For [Item] in (' + Stuff((Select Distinct concat(',[seating_id_',seating_id,']') from YourTable For XML Path('')),1,1,'')
+','+
Stuff((Select Distinct concat(',[dept_id_',dept_id,']') from YourTable For XML Path('')),1,1,'')
+ ') ) p'
Exec(@SQL);
返回
生成的SQL如下所示
Select *
From (
Select month,B.*
From YourTable A
Cross Apply (values (concat('seating_id_',seating_id),seating_id)
,(concat('dept_id_',dept_id),dept_id)
) b(item,value)
) A
Pivot (count([Value]) For [Item] in ([seating_id_1],[seating_id_5],[seating_id_7],[seating_id_8],[dept_id_1],[dept_id_2],[dept_id_3],[dept_id_5],[dept_id_9]) ) p
肯定是一个很好的答案,但它假设我已经知道所有ID,并且没有添加新ID。是否有一种方法可以迭代每个列的所有可用ID?ID来自单独的表(外键关系),可以在SELECT语句中引用。是。但它必须是动态的,这确实是一个很好的答案,但它假设我已经知道所有ID,并且没有添加新ID。是否有一种方法可以迭代每个列的所有可用ID?ID来自单独的表(外键关系),可以在SELECT语句中引用。是。但它必须是动态的