SQL 查询根据列分组获取总人数
SQL query to get sum of headcount according to column grouping
以下查询用于查找特定部门的员工人数。这工作正常-
SELECT distinct --LVL2_ORG,
division,
district,
department,
Count(DISTINCT person_number)
over (
PARTITION BY department) AS dep_empl_count
FROM flattened_tree,
hr_org_unit_classifications_f houcf,
hr_all_organization_units_f haouf,
hr_organization_units_f_tl hauft,
per_all_assignments_m paam,
per_all_people_f papf
WHERE haouf.organization_id = flattened_tree.department_id
AND haouf.organization_id = houcf.organization_id
AND haouf.organization_id = hauft.organization_id
AND haouf.effective_start_date BETWEEN
houcf.effective_start_date AND houcf.effective_end_date
AND hauft.LANGUAGE = 'US'
AND hauft.effective_start_date = haouf.effective_start_date
AND hauft.effective_end_date = haouf.effective_end_date
AND houcf.classification_code = 'DEPARTMENT'
AND Trunc(SYSDATE) BETWEEN hauft.effective_start_date AND
hauft.effective_end_date
AND hauft.organization_id = paam.organization_id
AND paam.person_id = papf.person_id
AND paam.primary_assignment_flag = 'Y'
AND paam.assignment_type = 'E'
AND paam.assignment_status_type IN ( 'ACTIVE', 'SUSPENDED' )
AND paam.effective_latest_change = 'Y'
AND trunc(sysdate) between paam.effective_start_date and paam.effective_end_date
AND trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
ORDER BY division,district,department
输出看起来像 -
DIVISION DISTRICT DEPARTMENT JAN
CA Division Grande XYZ 0
CA Division Deer Deer Tubing 1
CA Division Deer XYZ Fracturing 0
CA Division Fracturing CAA Fracturing 3
US Division Neuquen Support 101
US Division Neuquen Cementing 3
US Division Corporate Fracturing 190
现在我想在同一个查询中计算部门和地区级别的总和。即同一查询中每个地区和每个部门的总人数(JAN 列)。这可能吗?
输出应该类似于 -
DIVISION DISTRICT DEPARTMENT JAN SUM(DIVISION) SUM(DISTRICT)
CA Division Grande XYZ 0 4 0
CA Division Deer Deer Tubing 1 4 1
CA Division Deer XYZ Fracturing 0 4 1
CA Division Fracturing CAA Fracturing 3 4 3
US Division Neuquen Support 101 294 104
US Division Neuquen Cementing 3 294 104
US Division Corporate Fracturing 190 294 190
所以从上面的输出 - 部门总和应该是一个部门(CA Division 和 US Division)中所有员工人数的总和
并且区的总和应该是鹿区 - 1 + 0 = 1 内乌肯区 101 + 3 = 104 这样。
而不是 1 个小节:
Count(DISTINCT person_number)
over (
PARTITION BY department) AS dep_empl_count
使用 3 项措施:
Count(DISTINCT person_number) over (PARTITION BY department) AS dep_empl_count,
Count(DISTINCT person_number) over (PARTITION BY division) AS div_empl_count,
Count(DISTINCT person_number) over (PARTITION BY district) AS dis_empl_count
以下查询用于查找特定部门的员工人数。这工作正常-
SELECT distinct --LVL2_ORG,
division,
district,
department,
Count(DISTINCT person_number)
over (
PARTITION BY department) AS dep_empl_count
FROM flattened_tree,
hr_org_unit_classifications_f houcf,
hr_all_organization_units_f haouf,
hr_organization_units_f_tl hauft,
per_all_assignments_m paam,
per_all_people_f papf
WHERE haouf.organization_id = flattened_tree.department_id
AND haouf.organization_id = houcf.organization_id
AND haouf.organization_id = hauft.organization_id
AND haouf.effective_start_date BETWEEN
houcf.effective_start_date AND houcf.effective_end_date
AND hauft.LANGUAGE = 'US'
AND hauft.effective_start_date = haouf.effective_start_date
AND hauft.effective_end_date = haouf.effective_end_date
AND houcf.classification_code = 'DEPARTMENT'
AND Trunc(SYSDATE) BETWEEN hauft.effective_start_date AND
hauft.effective_end_date
AND hauft.organization_id = paam.organization_id
AND paam.person_id = papf.person_id
AND paam.primary_assignment_flag = 'Y'
AND paam.assignment_type = 'E'
AND paam.assignment_status_type IN ( 'ACTIVE', 'SUSPENDED' )
AND paam.effective_latest_change = 'Y'
AND trunc(sysdate) between paam.effective_start_date and paam.effective_end_date
AND trunc(sysdate) between papf.effective_start_date and papf.effective_end_date
ORDER BY division,district,department
输出看起来像 -
DIVISION DISTRICT DEPARTMENT JAN
CA Division Grande XYZ 0
CA Division Deer Deer Tubing 1
CA Division Deer XYZ Fracturing 0
CA Division Fracturing CAA Fracturing 3
US Division Neuquen Support 101
US Division Neuquen Cementing 3
US Division Corporate Fracturing 190
现在我想在同一个查询中计算部门和地区级别的总和。即同一查询中每个地区和每个部门的总人数(JAN 列)。这可能吗?
输出应该类似于 -
DIVISION DISTRICT DEPARTMENT JAN SUM(DIVISION) SUM(DISTRICT)
CA Division Grande XYZ 0 4 0
CA Division Deer Deer Tubing 1 4 1
CA Division Deer XYZ Fracturing 0 4 1
CA Division Fracturing CAA Fracturing 3 4 3
US Division Neuquen Support 101 294 104
US Division Neuquen Cementing 3 294 104
US Division Corporate Fracturing 190 294 190
所以从上面的输出 - 部门总和应该是一个部门(CA Division 和 US Division)中所有员工人数的总和 并且区的总和应该是鹿区 - 1 + 0 = 1 内乌肯区 101 + 3 = 104 这样。
而不是 1 个小节:
Count(DISTINCT person_number)
over (
PARTITION BY department) AS dep_empl_count
使用 3 项措施:
Count(DISTINCT person_number) over (PARTITION BY department) AS dep_empl_count,
Count(DISTINCT person_number) over (PARTITION BY division) AS div_empl_count,
Count(DISTINCT person_number) over (PARTITION BY district) AS dis_empl_count