[thelist] SQL: Select first item for each person for each day

Judah McAuley judah at wiredotter.com
Tue Feb 24 21:53:28 CST 2009


Thanks Rudy, I had to add an additional Group By in the derived table
you're joining on to be just the appointments for that day, but yeah,
all the basic logic is there. Thanks mate.

Judah

On Tue, Feb 24, 2009 at 5:43 PM, r937 <rudy at r937.com> wrote:
> hi judah
>
> try this --
>
> SELECT appt.id
>     , appt.patient_id
>     , appt.doctor
>     , appt.appointment_date
>  FROM Appointment AS appt
> INNER
>  JOIN ( SELECT patient_id
>              , MIN(appointment_date) AS first_appt
>           FROM Appointment
>         GROUP
>             BY patient_id
>       ) AS firsts
>    ON firsts.patient_id = appt.patient_id
>   AND firsts.first_appt = appt.appointment_date
>
> ;o)
>
> -------------
> rudy
> http://r937.com/ | http://rudy.ca/
> buy my book "Simply SQL" from Amazon --
> http://www.amazon.com/exec/obidos/ASIN/0980455251/
>
>
> --
>
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
>



More information about the thelist mailing list