GROUP_BY WITH DISTINCT QUERY

旧时模样 提交于 2019-12-13 07:59:07

问题


Am using Oracle 12c, below is the column:

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

From record 1 to 6, it contains 2 distinct employee_id who studied unit 4. In this case, I need total minutes spent per unit (center_id wise).

But I want to add only distinct MINS_STUDIED based on EMPLOYEE_ID.

So, total minutes spent for center_id I234 for unit 4 is (5+5)=10 and for center_id I2345 for unit 5 is 12.

I used the below query

    SELECT 
         COUNT(DISTINCT EMPLOYEE_ID),CENTER_ID,UNIT,
         SUM(DISTINCT MINS_STUDIED)
    FROM 
         TEST_DATA
    GROUP BY 
          CENTER_ID,DBD.UNIT;

The above query returns

center_id    UNIT  DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234          4      2                       5 
I2345         5      1                       12 

But, I need the output like below:

center_id    UNIT  DISTINCT(EMPLOYEE_ID) TOTAL_MINS_STUDIED
I234          4      2                       10 
I2345         5      1                       12

回答1:


If we assume you only have two employeeID's and the last entry is a typo..

Simply get a distinct list before you calculate totals.

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

Giving us: I234, 4, 2, 10

or using an analytic: though I'm not sure how to handle what hours_studied to sum if they very by employeeID... so the ordering may be incorrect.

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


来源:https://stackoverflow.com/questions/47266337/group-by-with-distinct-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!