0

I'm having trouble with a particular Oracle query. I'm trying to write a query to pull the following data.

• “How many students who were previously suspended for reason of "bad behaviour" have since been re-assessed AND the number over 12 years old and under 12 years old”

Three tables I believe I need to work with: person status history (contains REASON, SUSPEND_TO_DATE), person_visit (contains fields like VISIT_DATE) and person (contains fields such as PERSON_ID and BIRTH_DATE).

(PERSON_ID is common across all tables.)

Here was my amateur attempt, I'm still not fully sure of the use of joins & how to work out the over 12/under 12 using only the BIRTH_DATE information is another issue.

select count (*) from person_status_history 
inner join person_visit on person_status_history.person_id=person_visit.person_id
inner join person on person_visit.person_id=person.person_id
where person_status_history.reason_code = 85 and person_visit.reasses_appoint_no is not null and person.birth_date < '23-JAN-2000';

In basic pseudo-code I suppose what I need to develop is (very rough!):

SELECT * PERSON_ID WHERE SUSPEND_TO_DATE is NOT NULL AND REASON = "85" AND VISIT_DATE > SUSPEND_TO_DATE and count the number >12 AND < 12.

Can anyone advise?

4

2 回答 2

2

将其视为:“对于哪些学生存在停学,随后将进行访问” ...

select case when p.date_of_birth < current_date - interval '12 year'
       then '< 12'
       else '12+'
       end age_bracket,
       count (*)
from   person p
where  exists (
         select null
         from   person_status_history psh
         where  psh.person_id   = p.person_id and
                psh.reason_code = 85          and
                exists (
                  select null
                  from   person_visit pv
                  where  pv.person_id          = psh.person_id and
                         pv.reasses_appoint_no is not null     and
                         pv.visit_date         > psh.suspend_to_date)
group by case when p.date_of_birth < current_date - interval '12 year'
         then '< 12'
         else '12+'
         end

虽然没有测试语法错误等。

于 2013-01-23T10:46:20.357 回答
0

尝试像这样转换您的日期:

person.birth_date < TO_DATE('23-JAN-2000','DD-MM-YYYY')
于 2013-01-23T10:09:43.190 回答