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

2009-02-24 Thread Judah McAuley
Thanks to everyone, I got it working. For the sake of posterity, here is what I came up with: SELECT a.* FROMappointment a INNER JOIN (SELECT patient_id, MIN(appointment_date) AS appointment_date FROM appointment GROUP BY patient_id, convert(va

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

2009-02-24 Thread Dan Baughman
Isn't this way easier and executes way faster? select * from appointment left join ( select patient_id, min(appointment_date) as firstAppt, datepart(yy,appointment_date), datepart(dd,appointment_date), datepart(mm,appointment_date) from appointment group by p

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

2009-02-24 Thread Judah McAuley
I like this approach. That middle query inserting into the temp table might be what I need for the view. Thanks, Judah On Tue, Feb 24, 2009 at 12:48 PM, wrote: > > These sort of queries are always kind of annoying.  My approach is to to > use an intermediate temp table, group by patient and da

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
These sort of queries are always kind of annoying. My approach is to to use an intermediate temp table, group by patient and day, and find the min() appointment date. Then join that temp table back to your appointment table to pull out the rest of the information for that first appointment based