使用Oracle 12c,下面是专栏:
CENTER_ID UNIT EMPLOYEEID LESSON MINS_STUDIED SECTION
I234 4 G01234 4.1 5 EX
I234 4 G01234 4.1 5 LN
I234 4 G01234 4.1 5 VO
I234 4 G0123 4.2 5 EX
I234 4 G0123 4.2 5 LN
I234 4 G0123 4.2 5 VO
I2345 5 G023 5.2 12 EX
I2345 5 G023 5.2 12 LN
I2345 5 G023 5.2 12 VO
从记录1到6,它包含2个不同的employee_id,他们研究了单元4.在这种情况下,我需要每单位花费的总分钟数(center_id wise)。
但我想根据EMPLOYEE_ID只添加不同的MINS_STUDIED。
因此,第4单元的center_id I234所花费的总时间为(5 + 5)= 10,而第5单元的center_id I2345为12。
我使用了以下查询
SELECT
COUNT(DISTINCT EMPLOYEE_ID),CENTER_ID,UNIT,
SUM(DISTINCT MINS_STUDIED)
FROM
TEST_DATA
GROUP BY
CENTER_ID,DBD.UNIT;
以上查询返回
center_id UNIT DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234 4 2 5
I2345 5 1 12
但是,我需要输出如下:
center_id UNIT DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234 4 2 10
I2345 5 1 12
答案 0 :(得分:2)
如果我们假设您只有两个employeeID,而且最后一个条目是拼写错误..
在计算总数之前,只需获得一个不同的列表。
SELECT center_ID, unit, count(EmployeeID) cnt, sum(hours_Studied) sumHrs
FROM (SELECT distinct Center_ID, Unit, EmployeeID, hours_Studied
FROM test_data) TD
GROUP BY CENTER_ID, unit
给我们:I234, 4, 2, 10
或使用解析:虽然我不确定如何处理小时数达到总和,如果他们非常通过employeeID ......所以排序可能不正确。
SELECT Center_ID, Unit, count(Distinct EmployeeID) cnt_Dist_Emp , sum(Hours_Studied) sum_hrs
FROM (Select row_number() over (PARTITION BY center_ID, unit, employeeID, Hours_Studied order by center_ID, unit, employeeID, Hours_Studied ) rn, TD.*
FROM Test_data td)
WHERE RN = 1
GROUP BY Center_ID, Unit