简体   繁体   中英

Divide one aggregate query by another aggregate query from a different table

I have a query that calculates the average daily headcount of each group within an organization, broken out by quarter. This query creates a result set of a single number for each quarter from Table_A.

I have a second query that calculates the n-count of attrition of each group within an organization, broken out by quarter. This query creates a result set of a single number for each quarter from Table_B.

Instead of producing two separate result sets, I want to write a single query that calls off of both tables and allows me to divide the attrition aggregate by the headcount aggregate result, allowing me to show an attrition rate in the final result set.

Suggestions?

Since I am creating aggregate select queries, I assume joins won't work? Could I write something in that had each of the two result sets become a row in a temporary table, that I then query to run the division process?

Thanks in advance!

UPDATE:

So the two select queries are made up of multiple sums, each creating it's own quarter column from 2010 - 2015. So, the attrition counter query looks like this:

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

And the average daily headcount is similar in nature, with a separate sum function for each quarter.

Given this, Dead Zone, how would I incorporate the strategy you suggested?

You didn't specify what type of SQL you're using. But this should work for Microsoft SQL Server, using Common Table Expressions (or CTE's).

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

You'll have to fill in your queries for Query1 and Query2. But this should give you what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM