简体   繁体   中英

Oracle Query with multiple records

I have a students table that has multiple columns, however, I'm only interested in the re-enrollment column and the exitcode column. When a student graduates they are given a code of 'W21', when they are promoted to the next graded they are given a code of 'P'. They will also have a corresponding record in the re-enrollment column that contains the date of registration.

I need to find all students with an exitcode of 'W21' and that have also been in the District for the last four years. So a student with a code of 'W21' that has only been in the District for 3 years wouldn't be pulled.

Lisa, Simpson   240011111111        20-AUG-07   W10
Lisa, Simpson   240011111111        18-AUG-08   W10
Lisa, Simpson   240011111111        18-AUG-09   W21
Bart, Simpson   240022222222        07-AUG-00   W10
Bart, Simpson   240022222222        09-AUG-01   W10
Bart, Simpson   240022222222        08-AUG-02   W10
Bart, Simpson   240022222222        11-AUG-03   W10
Bart, Simpson   240022222222        09-AUG-04   W10
Bart, Simpson   240022222222        08-AUG-05   W10
Bart, Simpson   240022222222        14-AUG-06   W10
Bart, Simpson   240022222222        20-AUG-07   W10
Bart, Simpson   240022222222        18-AUG-08   W10
Bart, Simpson   240022222222        18-AUG-09   W21
Homer, Simpson  240000333333        07-AUG-00   W10
Homer, Simpson  240000333333        09-AUG-01   W10
Homer, Simpson  240000333333        08-AUG-02   W10
Homer, Simpson  240000333333        11-AUG-03   W10
Homer, Simpson  240000333333        09-AUG-04   W10
Homer, Simpson  240000333333        08-AUG-05   W10
Homer, Simpson  240000333333        14-AUG-06   W10
Homer, Simpson  240000333333        20-AUG-07   W10
Homer, Simpson  240000333333        18-AUG-08   W10
Homer, Simpson  240000333333        18-AUG-09   NS
Homer, Simpson  240000333333        21-AUG-09   W21

Basically I would need only to pull Bart and Homer because they where in the district the four years before they graduated. Lisa wouldn't be pulled.

I'm stumped how to accomplish this and any help would be great.

Thanks

Something like this, it is not going to be very fast, but it will work:

select *
  from studentrecords
 where     exitcode = 'W21'
       and studentid in (select studentid
                           from studentrecords
                          where exitcode = 'W21'
                         intersect
                           select studentid
                             from studentrecords
                            where enrollmentyear >= :fouryearsago
                         group by studentid
                           having count (*) >= 4)

A group by with a having clause should suffice for this. Truncating the dates to year and counting distinct should solve for the number of years.

select
   e.student, e.sid, count(distinct trunc(dated,'YYYY')) num_years
from enrollments e
inner join (select distinct sid from enrollments where exitcode = 'W21' ) x on e.sid = x.sid
group by e.student, e.sid
having count(distinct trunc(dated,'YYYY')) > 3

+----+---------------+--------------+-----------+
|    |    STUDENT    |     SID      | NUM_YEARS |
+----+---------------+--------------+-----------+
|  1 | Bart,Simpson  | 240022222222 |        10 |
|  2 | Homer,Simpson | 240000333333 |        10 |
+----+---------------+--------------+-----------+

see it working at http://rextester.com/VOGNJ58311

IF the exitcode W21 is the maximum value found in that column then you could achieve the result in a single pass of the data:

select
   e.student, e.sid, count(distinct trunc(dated,'YYYY')) num_years
from enrollments e
group by e.student, e.sid
having count(distinct trunc(dated,'YYYY')) > 3
And max(exitcode) = 'W21'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM