Subject Re: [firebird-support] Getting the Bigger Value of two fields in one field
Author Helen Borrie
At 10:53 PM 19/05/2005 +0200, you wrote:
>Hi.
>
>I have this Query:
>
>SELECT
> MAX("Treatments"."WorkStartDate") "WorkStartDate",
> MAX("TreatmentDetails"."TreatmentDetailDate") "TreatmentDetailDate"
>FROM
> "Treatments" LEFT OUTER JOIN "TreatmentDetails"
> ON "Treatments"."TreatmentID" = "TreatmentDetails"."TreatmentID"
>
>Is there away to get the bigger Date of those two fields in one fields and not I two as I did ?

Assuming some kind of grouping, e.g. PatientID and/or TreatmentID,

SELECT
"Treatments"."PatientID',
"Treatments"."TreatmentID',
case
when ((MAX("Treatments"."WorkStartDate") >= MAX("TreatmentDetails"."TreatmentDetailDate")
or (MAX("TreatmentDetails"."TreatmentDetailDate") is null))
then MAX("Treatments"."WorkStartDate")
else MAX("TreatmentDetails"."TreatmentDetailDate") end
as "LastTreatmentDate"

FROM "Treatments"
LEFT JOIN "TreatmentDetails"
ON "Treatments"."TreatmentID" = "TreatmentDetails"."TreatmentID"

GROUP BY 1, 2

(or whatever you need...)

./hb