Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Conditional counting of rows

MUmarAliNov 23 2016 — edited Nov 25 2016

Hi guys...again caught up in an analytical query, initially which I thought would be quite easy but not at the end.

Sample data for this is:

With TestData as

(

    Select 2014 AS Yer, 201401 AS WeekNumber, 1 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201401 AS WeekNumber, 2 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201401 AS WeekNumber, 3 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201402 AS WeekNumber, 1 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201402 AS WeekNumber, 2 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201402 AS WeekNumber, 3 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201402 AS WeekNumber, 4 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201403 AS WeekNumber, 1 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201403 AS WeekNumber, 1 AS Student, 'B' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201403 AS WeekNumber, 2 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201403 AS WeekNumber, 5 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201404 AS WeekNumber, 1 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201404 AS WeekNumber, 1 AS Student, 'B' AS Qualification From Dual

    Union All

    Select 2014 AS Yer, 201404 AS WeekNumber, 2 AS Student, 'A' AS Qualification From Dual

    Union All

    Select 2015 AS Yer, 201501 AS WeekNumber, 4 AS Student, 'C' AS Qualification From Dual

    Union All

    Select 2015 AS Yer, 201501 AS WeekNumber, 6 AS Student, 'A' AS Qualification From Dual

  Union All

    Select 2015 AS Yer, 201501 AS WeekNumber, 7 AS Student, 'D' AS Qualification From Dual

    Union All

    Select 2015 AS Yer, 201501 AS WeekNumber, 8 AS Student, 'M' AS Qualification From Dual

)

Select *

From TestData

Order By 1, 2, 3, 4

And the required data is:

  

YERWEEKNUMBERNewStudents
20142014013
20142014021
20142014032
20142014040
20152015014

Actually the results should be based on the enrollment of the new students every week. New Students mean the students who have not been enrolled in the same Qualification previously. You can judge this from sample data. If a student appears again in the next week with the same qualification, he is not counted. Therefore only students with new number or the students with new qualification will be counted.

I've tried to use Count(StudentNumber) Over(Partition By WeekNumber Order By WeekNumber Rows Unbounded Preceding)

and few of it's variations but not giving me correct results.

Can you help me for this?

Message was edited by: MUmarAli

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 23 2016
Added on Nov 23 2016
8 comments
959 views