Reputation: 1203
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?
Upvotes: 0
Views: 208
Reputation: 52376
Thinking about this as: "For which students does there exist a suspension for which there is a subsequent visit" ...
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
Not tested for syntax errors etc though.
Upvotes: 2
Reputation: 8451
try to convert your date like this:
person.birth_date < TO_DATE('23-JAN-2000','DD-MM-YYYY')
Upvotes: 0