使用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 :(得分:0)
数据似乎需要忽略重复(由“SECTION”列引起)
SELECT
CENTER_ID
, UNIT
, COUNT(DISTINCT EMPLOYEE_ID)
, SUM(MINS_STUDIED)
FROM (
select distinct CENTER_ID, UNIT, EMPLOYEEID , MINS_STUDIED
from TEST_DATA
) t
GROUP BY
CENTER_ID,UNIT;
答案 1 :(得分:0)
您可以使用分析功能执行此操作,无需子选择和分组:
编辑,将Employee_id添加到SUM函数的partititon by子句
SELECT
CENTER_ID, UNIT,
COUNT(EMPLOYEE_ID) OVER (PARTITION BY CENTER_ID, UNIT) EMPLOYEES,
SUM(MIN_STUDIED) OVER (PARTITION BY CENTER_ID, UNIT, EMPLOYEE_ID) TOTAL_MINS_STUDIED
FROM
TESTS_DATA
ORDER BY
CENTER_ID, UNIT;