Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Problems with GROUP BY

Hello-

I am working with a database that stores information about medical cases.
There is a table of cases which holds info on the physicians and patient
and another table of reports which holds the text of the report and some
other info. Reports are sometimes revised, but we keep records of the old
versions for legal reasons. All reports in a case are given sequential version
numbers, so the newest report in a case is the one with the highest version
number. I need to write a query that returns the text of only the most recent
reports on a given patient.

I was thinking I could do something like:

SELECT Reports.ReportText
FROM Reports INNER JOIN Cases ON Reports.CaseID = Cases.CaseID
WHERE Cases.PatientID = (search criteria)
GROUP BY CaseID
HAVING Reports.Version = MAX(Reports.Version)

Unfortunately this won't work because I can't select ReportText without grouping
by ReportText. Any help would be much appreciated.

Thanks a lot,
Mike
[1058 byte] By [Mike Deck] at [2007-11-9 21:09:22]
# 1 Re: Problems with GROUP BY
I've used sub-selects for things like this.

Something like the following.

SELECT Reports.ReportText
FROM Reports INNER JOIN Cases ON Reports.CaseID = Cases.CaseID
WHERE Cases.PatientID = xxxxx
AND Reports.Version in (
SELECT MAX(Reports.Version)
FROM Reports INNER JOIN Cases ON Reports.CaseID = Cases.CaseID
WHERE Cases.PatientID = xxxxx)

Hopefully the idea behind the sub-select comes across in the above.

Kevin

"Mike Deck" <MichaelBDeck@TexasHealth.org> wrote:
>
>Hello-
>
>I am working with a database that stores information about medical cases.
> There is a table of cases which holds info on the physicians and patient
>and another table of reports which holds the text of the report and some
>other info. Reports are sometimes revised, but we keep records of the old
>versions for legal reasons. All reports in a case are given sequential
version
>numbers, so the newest report in a case is the one with the highest version
>number. I need to write a query that returns the text of only the most
recent
>reports on a given patient.
>
>I was thinking I could do something like:
>
>SELECT Reports.ReportText
>FROM Reports INNER JOIN Cases ON Reports.CaseID = Cases.CaseID
>WHERE Cases.PatientID = (search criteria)
>GROUP BY CaseID
>HAVING Reports.Version = MAX(Reports.Version)
>
>Unfortunately this won't work because I can't select ReportText without
grouping
>by ReportText. Any help would be much appreciated.
>
>Thanks a lot,
>Mike
Kevin at 2007-11-11 23:53:53 >
# 2 Re: Problems with GROUP BY
"Mike Deck" <MichaelBDeck@TexasHealth.org> wrote in message <news:3d4e9b23$1@10.1.10.29>...

> numbers, so the newest report in a case is the one with the highest version
> number. I need to write a query that returns the text of only the most recent
> reports on a given patient.
>
> I was thinking I could do something like:
>
> SELECT Reports.ReportText
> FROM Reports INNER JOIN Cases ON Reports.CaseID = Cases.CaseID
> WHERE Cases.PatientID = (search criteria)
> GROUP BY CaseID
> HAVING Reports.Version = MAX(Reports.Version)
>
> Unfortunately this won't work because I can't select ReportText without grouping
> by ReportText. Any help would be much appreciated.

Close! This should get you the most recent report per case:

select reports.caseid, reports.version,
max(reports.reporttext) as reporttext
from reports inner join reports as r on reports.caseid = r.caseid
group by reports.caseid, reports.version
having reports.version = max(r.version)

It shouldn't make any difference whether you use min() or max()
on Reports.ReportText. It's just to get the SQL parser to STFU.

Now add the Cases table back in:

select reports.caseid, reports.version,
max(reports.reporttext) as reporttext
from (reports inner join reports as r on reports.caseid = r.caseid)
inner join cases on reports.caseid = cases.caseid
where cases.patientid = bork bork bork
group by reports.caseid, reports.version
having reports.version = max(r.version)

--
Joe Foster <mailto:jlfoster%40znet.com> Space Cooties! <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above They're coming to
because my cats have apparently learned to type. take me away, ha ha!
Joe \Nuke Me Xemu\ Foster at 2007-11-11 23:54:53 >