简体   繁体   English

具有多个记录的Oracle Query

[英]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. 我的学生表有多个列,但是,我只对重新注册列和exitcode列感兴趣。 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'. 当学生毕业时,他们的代码为“ W21”,当他们升入下一年级时,他们的代码为“ 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. 我需要找到所有退出代码为“ W21”并且过去四年也一直在学区学习的学生。 So a student with a code of 'W21' that has only been in the District for 3 years wouldn't be pulled. 因此,代码为“ W21”且仅在本学区呆了3年的学生不会被拉走。

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. 基本上,我只需要拉Bart和Homer,因为他们毕业前的四年在该地区。 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. 具有having子句的group by应该足够了。 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 参见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: 如果退出码W21是在该列中找到的最大值,那么您可以通过一次传递数据来获得结果:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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