i need some assistance with a SQL query:
the data looks like this:
ID (int, PK)
REF (int, FK)
UserName (varchar)
TransDate (DateTime)
Status (int)
1, 1001, joebloggs, 2011-12-15 13:50:01, 1
2, 1001, johnsmith, 2011-12-15 14:35:01, 1
so i need to return the REF and Username for the following; MIN(TransDate) and where status = 1.
so i do the following:
SELECT REF, UserName, MIN(TransDate )
FROM dbo.MyTable
WHERE (Status = 1)
and REF = 1001
GROUP BY REF, UserName
obviously this is wrong because it returns me:
1001, joebloggs, 2011-12-15 13:50:01
1001, johnsmith, 2011-12-15 14:35:01
however, i just need to return the username of the min transDate. so:
1001, joebloggs, 2011-12-15 13:50:01
can anyone help me please..its driving me insane.
thanks (from my sanity)
EDIT:
Or, if you need the results for each REF, instead of a specific REF, you can try this: