Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

oracle - GROUP_BY WITH DISTINCT QUERY

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...