ID TestDate Score1 Status1 Score2 Status2
11111 5/10/05 325 N R
11111 9/1/06 330 N R
22222 2/8/05 300 N 325 N
22222 5/10/05 360 P 365 P
ID TestDate Score1 Status1 Score2 Status2
11111 9/1/06 330 N R
22222 5/10/05 360 P 365 P
select
ID, max(TestDate), Status1, Score1, Status2, Score2
from myTable
group by ID, Status1, Score1, Status2, Score2
SELECT ID testDate, Status1, Score1, Status2, Score2 FROM myTable ORDER BY testDate LIMIT 1
select * from myTable join ( select ID, max(testDate) as testDate from myTable group by ID ) as myTableAlias using (ID, testDate)# explain select * from myTable a join ( select ID, max(testDate) as testDate from myTable group by ID ) b using (ID, testDate);
QUERY PLAN
-------------------------------------------------------------------------------------
Merge Join (cost=159.86..173.66 rows=8 width=16)
Merge Cond: (("outer".testdate = "inner".testdate) AND ("outer".id = "inner".id))
-> Sort (cost=113.27..117.34 rows=1630 width=16)
Sort Key: a.testdate, a.id
-> Seq Scan on mytable a (cost=0.00..26.30 rows=1630 width=16)
-> Sort (cost=46.59..47.09 rows=200 width=12)
Sort Key: b.testdate, b.id
-> HashAggregate (cost=34.45..36.95 rows=200 width=12)
-> Seq Scan on mytable (cost=0.00..26.30 rows=1630 width=12)
(9 rows)
# explain select * from myTable a where testDate = ( select max(testDate) as testDate from myTable b where a.ID = b.ID );
QUERY PLAN
------------------------------------------------------------------------
Seq Scan on mytable a (cost=0.00..49594.60 rows=8 width=16)
Filter: (testdate = (subplan))
SubPlan
-> Aggregate (cost=30.40..30.41 rows=1 width=8)
-> Seq Scan on mytable b (cost=0.00..30.38 rows=8 width=8)
Filter: ($0 = id)
(6 rows)
Something like this will be closer to what you need:
Select ID, testDate, Status1, Score1, Status2, Score2
from myTable a
where testDate = (select max(testDate) from myTable b
where a.ID = b.ID)
The sub-query picks the maximum test date for the ID, and then the main query picks the line from the list that has that particular date on it.
Depending on how your database is set up, this may prove vastly inefficient, which only really matters if its a fairly large dataset. In that case, it might be better to temporarily material a table of ID, max(testdate) pairs and join the two tables together.
posted by jacquilynne at 2:40 PM on November 21, 2006 [1 favorite has favorites]