 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Crazy Horse's crazier lit Guest
|
Posted: Mon Mar 28, 2005 7:35 pm Post subject: Need SQL help |
|
|
Help me, Rhonda! (or...),
I want to retrieve a list of patients currently in the hospital. I can do
this (I think) by comparing admit dates with departure dates-every patient
who has an admit_date that is later than their most recent departure date is
here. However, the SQL I've cobbled together is bad:
I've tried all the derivations of the following I can think of, and nothing
works:
select p.firstname, p.lastname, a.admit_date, a.ADMISSION_ID,
a.ADMIT_ORDINAL, d.departure_date
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
where max(a.admit_date) > max(d.DEPARTURE_DATE)
--if I replace "where" with "having," that also fails.
--
Download Blackbird Crow Raven's book
"STILL CASTING SHADOWS: Two American Families 1620-2006"
here: http://www.winsite.com/bin/Info?22000000036712
|
|
| Back to top |
|
 |
Eric Hill Guest
|
Posted: Mon Mar 28, 2005 8:20 pm Post subject: Re: Need SQL help |
|
|
Is the departure date filled in if the patient is still in the hospital?
select * from ... where departure_date is null
Eric
|
|
| Back to top |
|
 |
Iman L Crawford Guest
|
Posted: Mon Mar 28, 2005 9:42 pm Post subject: Re: Need SQL help |
|
|
"Crazy Horse's crazier little brother" <cshannon (AT) d4sw (DOT) com> wrote in
news:42485c8f$1 (AT) newsgroups (DOT) borland.com:
| Quote: | select p.firstname, p.lastname, a.admit_date, a.ADMISSION_ID,
a.ADMIT_ORDINAL, d.departure_date
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
where max(a.admit_date) > max(d.DEPARTURE_DATE)
|
IIRC, you need a group by if you're using an aggregate function.
--
Iman
|
|
| Back to top |
|
 |
Barak zabari Guest
|
Posted: Tue Mar 29, 2005 9:12 am Post subject: Re: Need SQL help |
|
|
I assume that for each patient you have one record in admission table and
one record in departure for each visit to the hospital (each Admission_Id)
I assume that since the patient is still at the hospitale then you don't
need the departure date (since he didn't left yet) base on that i think this
should work
select p.firstname, p.lastname, a.admit_date, a.ADMISSION_ID,
a.ADMIT_ORDINAL
from patient_admissions a
join patients p on p.PATIENT_ID = a.admission_id
Where not Exits (Select * from departures D Where d.admission_id =
a.ADMISSION_ID
And A.admit_date <d.DEPARTURE_DATE
And d.DEPARTURE_DATE is not null)
"Crazy Horse's crazier little brother"
| Quote: | Help me, Rhonda! (or...),
I want to retrieve a list of patients currently in the hospital. I can do
this (I think) by comparing admit dates with departure dates-every patient
who has an admit_date that is later than their most recent departure date
is
here. However, the SQL I've cobbled together is bad:
I've tried all the derivations of the following I can think of, and
nothing
works:
select p.firstname, p.lastname, a.admit_date, a.ADMISSION_ID,
a.ADMIT_ORDINAL, d.departure_date
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
where max(a.admit_date) > max(d.DEPARTURE_DATE)
--if I replace "where" with "having," that also fails.
--
Download Blackbird Crow Raven's book
"STILL CASTING SHADOWS: Two American Families 1620-2006"
here: http://www.winsite.com/bin/Info?22000000036712
|
|
|
| Back to top |
|
 |
Wayne Niddery [TeamB] Guest
|
Posted: Tue Mar 29, 2005 3:51 pm Post subject: Re: Need SQL help |
|
|
Crazy Horse's crazier little brother wrote:
| Quote: |
I want to retrieve a list of patients currently in the hospital. I
can do this (I think) by comparing admit dates with departure
dates-every patient who has an admit_date that is later than their
most recent departure date is here. However, the SQL I've cobbled
together is bad:
select p.firstname, p.lastname, a.admit_date, a.ADMISSION_ID,
a.ADMIT_ORDINAL, d.departure_date
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
where max(a.admit_date) > max(d.DEPARTURE_DATE)
|
What would make this easy is if there was some direct *unique* link between
admissions and departures (i.e. not the patient id, but a separate admission
id that is stored in the matching departure record). Then the SQL would be:
select <fields>
from patient_admissions a
join patients p on p.PATIENT_ID = a.PATIENTID
where not exists (
select 1 from departures d
where d.admissionid = a.admission_id
)
Assuming you do not have such a link then try:
select p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL,
MAX(a.admit_date), MAX(d.departure_date)
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
GROUP BY p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL
where max(a.admit_date) > max(d.DEPARTURE_DATE)
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"The two most abundant elements in the universe are hydrogen and
stupidity." - Harlan Ellison
|
|
| Back to top |
|
 |
Crazy Horse's crazier lit Guest
|
Posted: Tue Mar 29, 2005 4:15 pm Post subject: Re: Need SQL help |
|
|
"Wayne Niddery [TeamB]" <wniddery (AT) chaffaci (DOT) on.ca> wrote
| Quote: |
Assuming you do not have such a link then try:
select p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL,
MAX(a.admit_date), MAX(d.departure_date)
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
GROUP BY p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL
where max(a.admit_date) > max(d.DEPARTURE_DATE)
|
The latter SQL (quoted above) looked interesting, but when I plugged it into
DBWB, I got "token unknown - line 7, char 2 where"
--
Download Blackbird Crow Raven's book
"STILL CASTING SHADOWS: Two American Families 1620-2006"
here: http://www.winsite.com/bin/Info?22000000036712
|
|
| Back to top |
|
 |
Eric Hill Guest
|
Posted: Tue Mar 29, 2005 4:23 pm Post subject: Re: Need SQL help |
|
|
| Quote: | The latter SQL (quoted above) looked interesting, but when I plugged it into
DBWB, I got "token unknown - line 7, char 2 where"
|
Put the WHERE clause before the GROUP BY clause.
Eric
|
|
| Back to top |
|
 |
Patrick Guest
|
Posted: Tue Mar 29, 2005 4:42 pm Post subject: Re: Need SQL help |
|
|
And change it to HAVING
"Eric Hill" <eric (AT) ijack (DOT) net> wrote
| Quote: | The latter SQL (quoted above) looked interesting, but when I plugged it
into
DBWB, I got "token unknown - line 7, char 2 where"
Put the WHERE clause before the GROUP BY clause.
Eric
|
|
|
| Back to top |
|
 |
Crazy Horse's crazier lit Guest
|
Posted: Tue Mar 29, 2005 5:05 pm Post subject: Re: Need SQL help |
|
|
"Patrick" <pdc (AT) novusprintmedia (DOT) com> wrote
| Quote: | And change it to HAVING
"Eric Hill" <eric (AT) ijack (DOT) net> wrote in message
news:42498105 (AT) newsgroups (DOT) borland.com...
The latter SQL (quoted above) looked interesting, but when I plugged it
into
DBWB, I got "token unknown - line 7, char 2 where"
Put the WHERE clause before the GROUP BY clause.
|
Then* I get "token unknown - line 7, char 2 group"
* select p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL,
MAX(a.admit_date), MAX(d.departure_date)
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
having max(a.admit_date) > max(d.DEPARTURE_DATE)
GROUP BY p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL
--
Download Blackbird Crow Raven's book
"STILL CASTING SHADOWS: Two American Families 1620-2006"
here: http://www.winsite.com/bin/Info?22000000036712
|
|
| Back to top |
|
 |
Iman L Crawford Guest
|
Posted: Tue Mar 29, 2005 5:24 pm Post subject: Re: Need SQL help |
|
|
"Crazy Horse's crazier little brother" <cshannon (AT) d4sw (DOT) com> wrote in
news:42498acc (AT) newsgroups (DOT) borland.com:
| Quote: | Then* I get "token unknown - line 7, char 2 group"
|
HAVING clause follows the GROUP BY clause. You really should get a
beginners guide to SQL, preferrably one directed toward the server you're
using.
--
Iman
|
|
| Back to top |
|
 |
Wayne Niddery [TeamB] Guest
|
Posted: Wed Mar 30, 2005 4:30 pm Post subject: Re: Need SQL help |
|
|
Crazy Horse's crazier little brother wrote:
| Quote: | Then* I get "token unknown - line 7, char 2 group"
* select p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL,
MAX(a.admit_date), MAX(d.departure_date)
from patient_admissions a
join departures d on d.admission_id = a.ADMISSION_ID
join patients p on p.PATIENT_ID = a.admission_id
GROUP BY p.firstname, p.lastname, a.ADMISSION_ID, a.ADMIT_ORDINAL |
having max(a.admit_date) > max(d.DEPARTURE_DATE)
--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"The moment the idea is admitted into society that property is not as
sacred as the laws of God and there is not a force of law and public
justice to protect it, anarchy and tyranny commence." - John Adams
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|