Re: Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread Judah McAuley
02-25 7:00' >> union all select 2, 'McSteamy', '2009-02-25 7:45' >> >> insert into @first_appointment_per_day >> (patient_id, appointment_date) >> (select patient_id, min(appointment_date) >> from @appointment >> group by patient_id, conv

Re: Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread Dan Baughman
-25 7:00' > > union all select 2, 'McSteamy', '2009-02-25 7:45' > > > > insert into @first_appointment_per_day > > (patient_id, appointment_date) > > (select patient_id, min(appointment_date) > > from @appointment > > group by patient

Re: Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread Judah McAuley
t patient_id, min(appointment_date) > from @appointment > group by patient_id, convert(varchar(10), appointment_date, 101)) > > select app.patient_id, app.appointment_date, app.doctor > from @first_appointment_per_day tmp1 > inner join @appointment app on tmp1.patient_id = app.patient_id &

Re: Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread Judah McAuley
This is the sort of approach I was trying to think of, couldn't remember derived tables. This query only grabs the earliest appointment for each patient though not the earliest for every day. But it is a good starting point, I'll try to massage a group by date in there and see what I can come up w

Re: Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread Brian Peddle
Maybe Select top 1 * from appointment where appointment between (date/time and date/time) order by appointment_date Ben Conner wrote: > Hi Judah, > > I suppose there's more than one way to do this, but this should work... > > SELECT a.* > FROM appointment a INNER JOIN >

Re: Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread Ben Conner
Hi Judah, I suppose there's more than one way to do this, but this should work... SELECT a.* FROM appointment a INNER JOIN (SELECT patient_id, MIN(appointment_date) AS appointment_date FROM appointment

RE: Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread brad
ointment_date) from @appointment group by patient_id, convert(varchar(10), appointment_date, 101)) select app.patient_id, app.appointment_date, app.doctor from @first_appointment_per_day tmp1 inner join @appointment app on tmp1.patient_id = app.patient_id and tmp1.appointment_date = app.appointment_date ~Brad

Semi-OT: SQL question...Select first item for each person for each day

2009-02-24 Thread Judah McAuley
I'm ill and having difficulty wrapping my head around an issue I know I've done before, so I'm hoping that someone can help me out. I have a table that lists patient's appointments. Appointment id patient_id doctor appointment_date (datetime) A patient can have 0...n appointments o