My database table is something like this:
#tblMain ID Value CreatedDate ________________________________________ 1 25 2011-10-11 14:00:45.910 1 20 2011-10-26 14:00:12.910 2 27 2011-10-14 14:00:32.910 2 39 2011-10-14 14:00:28.910 2 54 2011-10-17 14:00:27.910 3 67 2011-10-25 14:00:16.910 3 79 2011-10-25 14:00:02.910 4 34 2011-10-26 14:00:14.910 4 24 2011-10-26 14:00:06.910 4 88 2011-10-26 14:00:47.910 5 12 2011-10-26 14:03:14.910 5 34 2011-10-26 14:04:06.910 5 55 2011-10-26 14:04:47.910
I’ll get a list of IDs from a different table. So now I want to join the above table to this table based on ID, in such a way that I’ll get 1 row for every distinct ID with a value field from the row with MIN(CreatedDate) i.e. oldest value for that particular ID. i.e. for every row, the selected row would be :
SELECT TOP(1) * from #tblMain ORDER BY CreatedDate ASC where ID = 1
SELECT TOP(1) * from #tblMain ORDER BY CreatedDate ASC where ID = 2...and so on.
Thus, my output should be like this:
ID Value CreatedDate X Y Z(other columns from other tables) _______________________________________________________________________________ 1 25 2011-10-11 14:00:45.910 2 39 2011-10-14 14:00:28.910 3 79 2011-10-25 14:00:02.910 4 24 2011-10-26 14:00:06.910 5 12 2011-10-26 14:03:14.910
Trust me, I’ve tried my best to present my requirements as clearly as I possibly can, still if there’s something unclear, let me know. Expecting a quick response. Thanks.
Try: