下面的查询是用来查询某个部门的员工人数。这是很好的工作
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部门和US部门)所有人数的总和,而地区的总和应该是Deer地区--1+0=1,Neuquen地区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