Let's say I have the following example table
GroupID ItemID Created
-----------------------------------------------
A ABC 5/1/2009 13:02
A XZY 5/1/2009 13:01
A LMO 5/1/2009 13:03
A DEF 5/1/2009 13:00
A PQR 5/1/2009 13:04
B WXY 5/1/2009 13:02
B HIJ 5/1/2009 13:03
B STU 5/1/2009 13:01
How can I return the first ItemID for each group based on the Created column? I need a query to return the following:
GroupID ItemID Created
-----------------------------------------------
A DEF 5/1/2009 13:00
B STU 5/1/2009 13:01
The data set I'm working with may have several thousand to a few million rows, so I'd like to avoid any costly subqueries if possible. I was thinking maybe I could do it with an inner join, but my initial attempts led to my own confusion.
Edit: So far, I have the following:
select t2.ItemID, count(*) from
(select GroupID, min(created) as created from table) t1
inner join table t2 on t1.created = t2.created
group by t2.itemid
Any reason this isn't going to work? It's slower than I'd like, but it's updating a summary table, so it's not a huge issue.