Sql 将一个聚合查询除以来自不同表的另一个聚合查询

Sql 将一个聚合查询除以来自不同表的另一个聚合查询,sql,Sql,我有一个查询,用于计算组织内每个组的平均每日员工人数,按季度划分。此查询从表a为每个季度创建一个单一数字的结果集 我还有第二个查询,它计算一个组织内每个组的损耗的n个计数,按季度划分。此查询从表B为每个季度创建一个单一数字的结果集 我不想生成两个单独的结果集,而是想编写一个查询,调用两个表,并允许我将人员流失聚合除以人员总数聚合结果,从而在最终结果集中显示人员流失率 建议 因为我正在创建聚合选择查询,所以我假设联接不会工作?我可以写一些东西,让两个结果集中的每一个都成为临时表中的一行,然后查询以

我有一个查询,用于计算组织内每个组的平均每日员工人数,按季度划分。此查询从表a为每个季度创建一个单一数字的结果集

我还有第二个查询,它计算一个组织内每个组的损耗的n个计数,按季度划分。此查询从表B为每个季度创建一个单一数字的结果集

我不想生成两个单独的结果集,而是想编写一个查询,调用两个表,并允许我将人员流失聚合除以人员总数聚合结果,从而在最终结果集中显示人员流失率

建议

因为我正在创建聚合选择查询,所以我假设联接不会工作?我可以写一些东西,让两个结果集中的每一个都成为临时表中的一行,然后查询以运行除法过程吗

提前谢谢

更新:

因此,这两个select查询由多个总和组成,每个总和都创建了自己的2010-2015年季度列。因此,损耗计数器查询如下所示:

select 
case when unified_rollup_level_2 = "Group A" then "A" 
     when unified_rollup_level_2 = "Group B" and job_family_code <> "PROD_MGT" then "B" 
     when unified_rollup_level_2 = "Group C" or job_family_code = "PROD_MGT" then "C"
     when unified_rollup_level_2 = "Group D" then "D" end as [Group],
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 1 and year(termination_date) = 2010 then 1 else 0 end) Q1_2010,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 2 and year(termination_date) = 2010 then 1 else 0 end) Q2_2010,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 3 and year(termination_date) = 2010 then 1 else 0 end) Q3_2010,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 4 and year(termination_date) = 2010 then 1 else 0 end) Q4_2010,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 1 and year(termination_date) = 2011 then 1 else 0 end) Q1_2011,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 2 and year(termination_date) = 2011 then 1 else 0 end) Q2_2011,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 3 and year(termination_date) = 2011 then 1 else 0 end) Q3_2011,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 4 and year(termination_date) = 2011 then 1 else 0 end) Q4_2011,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 1 and year(termination_date) = 2012 then 1 else 0 end) Q1_2012,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 2 and year(termination_date) = 2012 then 1 else 0 end) Q2_2012,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 3 and year(termination_date) = 2012 then 1 else 0 end) Q3_2012,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 4 and year(termination_date) = 2012 then 1 else 0 end) Q4_2012,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 1 and year(termination_date) = 2013 then 1 else 0 end) Q1_2013,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 2 and year(termination_date) = 2013 then 1 else 0 end) Q2_2013,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 3 and year(termination_date) = 2013 then 1 else 0 end) Q3_2013,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 4 and year(termination_date) = 2013 then 1 else 0 end) Q4_2013,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 1 and year(termination_date) = 2014 then 1 else 0 end) Q1_2014,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 2 and year(termination_date) = 2014 then 1 else 0 end) Q2_2014,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 3 and year(termination_date) = 2014 then 1 else 0 end) Q3_2014,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 4 and year(termination_date) = 2014 then 1 else 0 end) Q4_2014,
sum(case when unified_rollup_level_1 = "Organization A" and person_type = 'Employee' and termination_counter = 1 and quarterofyear(termination_date) = 1 and year(termination_date) = 2015 then 1 else 0 end) Q1_2015
#copy row above and adjust Month and Year to add in current Quarter's data
from TerminationDetail
group by Group
having Group IN ("A","B","C","D")
order by Group asc
选择
统一\u汇总\u级别\u 2=“A组”然后“A”时的情况
当统一汇总\u level\u 2=“B组”和作业\u系列\u代码为“产品管理”时,则为“B”
当统一\u汇总\u级别\u 2=“C组”或作业\u系列\u代码=“生产管理”时,则为“C”
当统一\u汇总\u级别\u 2=“组D”时,则“D”结束为[组],
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=1且年(终止日期)=2010年,则为1,否则为0)2010年第1季度,
求和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=2且年(终止日期)=2010年第二季度时,则为1,否则为0结束),
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=3且年(终止日期)=2010年第3季度时,则为1,否则为0),
求和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=4且年(终止日期)=2010年第4季度时,则为1,否则为0),
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=1且年(终止日期)=2011年,则为1,否则为0)2011年第1季度,
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=2且年(终止日期)=2011年第二季度时,则为1,否则为0),
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=3且年年度(终止日期)=2011,则为1,否则为0)2011年第3季度,
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=4且年(终止日期)=2011年第4季度时,则为1,否则为0),
求和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=1且年年度(终止日期)=2012时,则为1,否则为0)2012年第1季度,
求和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=2且年年度(终止日期)=2012,则为1,否则为0)2012年第二季度,
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=3且年年度(终止日期)=2012时,则为1,否则为0)2012年第3季度,
求和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=4且年年度(终止日期)=2012时,则为1,否则为0)2012年第4季度,
求和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=1且年(终止日期)=2013年,则为1,否则为0)2013年第1季度,
合计(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=2且年(终止日期)=2013年第二季度时,则为1,否则为0),
2013年第三季度合计(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=3且年年度(终止日期)=2013时,则为1,否则为0),
合计(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=4且年(终止日期)=2013年第4季度时,则为1,否则为0),
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)为1且年(终止日期)为2014年第1季度时,则为1,否则为0),
2014年第二季度合计(当统一汇总层级为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)为2且年年度(终止日期)为2014,则为1,否则为0),
2014年第三季度合计(当统一汇总层级为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=3且年年度(终止日期)=2014时,则为1,否则为0),
2014年第4季度合计(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)=4且年年度(终止日期)=2014时,则为1,否则为0),
总和(当统一汇总级别为“组织A”且人员类型为“员工”且终止计数器为1且年季度(终止日期)为1且年(te)时的情况)
WITH 
  Query1 AS (SELECT Quarter, Headcount FROM...),
  Query2 AS (SELECT Quarter, Attrition FROM...)
SELECT Query1.Quarter, Query1.Headcount / Query2.Attrition AS AttritionRate
FROM Query1
INNER JOIN Query2 on Query2.Quarter = Query1.Quarter