| Author |
Topic |
|
SoonerAK
Starting Member
3 Posts |
Posted - 2011-09-15 : 23:13:16
|
| I have a table with 440K rows and running sql 2000. I like to select emp id max time and project that goes with the time. This is some sample data:Emp ID Time Project1 9/15/2011 17:51 A1 9/15/2011 18:41 B1 9/15/2011 21:31 C1 9/15/2011 21:38 A1 9/15/2011 22:33 C2 9/15/2011 12:31 B2 9/15/2011 13:01 C2 9/15/2011 15:31 T2 9/15/2011 16:38 T2 9/15/2011 22:31 T3 9/15/2011 16:30 A3 9/15/2011 15:30 B3 9/15/2011 14:31 E3 9/15/2011 12:11 D3 9/15/2011 12:02 CI want results to be like this:Emp ID Time Project1 9/15/2011 22:33 C2 9/15/2011 22:31 T3 9/15/2011 16:30 AThanks in advance!I thought about using sql like this:Select emp_id,time,projectfrom table1 awhere a.time = (select max(b.time) from table1 b where a.emp_id = b.emp_id)I'm not sure if this is the most efficient way of doing it since it will be used on a web based report. |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-09-16 : 00:15:22
|
| Sooner,The thing that is throwing me off is that you have the max time and project for empid 1 on a date prior to the max date.Are you only looking at the time portion of the datetime field for the "max" time? Or are you looking at the time and date? 9/16/2011 17:51 is later than 9/15/2011 22:33. |
 |
|
|
SoonerAK
Starting Member
3 Posts |
Posted - 2011-09-16 : 00:45:25
|
| Sorry, Flameblaster. I had a typo, the date should have been 15th not 16th, but I would consider the datetime when searching for Max. Thanks for the reply. |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-09-16 : 00:52:11
|
Ok that makes sense...I thought I was going crazy :)This should work...as always, I'm sure there are 30 people with less "wordy" solutions than mine, but this should do it:[CODE]select a.empid, b.[time], b.projectfrom (select empid, max(row) as Seqfrom (select empid, [time], project, ROW_NUMBER() over (partition by empid order by [time], project) as rowfrom table1) Xgroup by empid) Ajoin (select empid, [time], project, ROW_NUMBER() over (partition by empid order by [time], project) as row from table1) B on b.empid=a.empid and b.row=a.Seq[/CODE]Works but too complicated, see next post |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-09-16 : 01:13:59
|
| Ok, after being very irritated with myself for making this so difficult, I went back to the drawing board. This is much more efficient:[CODE]select empid, [time], projectfrom (select empid, [time], project, ROW_NUMBER() over (partition by empid order by [time] desc, project desc) as rowfrom table1) Awhere A.row=1[/CODE] |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-16 : 01:38:34
|
quote: Originally posted by flamblaster Ok, after being very irritated with myself for making this so difficult, I went back to the drawing board. This is much more efficient:[CODE]select empid, [time], projectfrom (select empid, [time], project, ROW_NUMBER() over (partition by empid order by [time] desc, project desc) as rowfrom table1) Awhere A.row=1[/CODE]
What i think is SQL 200o doesn't support row_number() function.iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
ashishashish
Constraint Violating Yak Guru
408 Posts |
Posted - 2011-09-16 : 01:46:06
|
| This will work in SQL Server 2000SELECT B.EmpID,B.Stime,A.Project FROM #temp1 AINNER JOIN(SELECT EmpID,MAX(time) as Stime FROM #temp1GROUP BY EmpID)BON A.Time=B.StimeHope this help.iF theRe iS a wAy iN tHen theRe iS a wAy oUt.. |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-09-16 : 01:59:25
|
| Ugh, sorry, not really all that familiar with 2000. |
 |
|
|
SoonerAK
Starting Member
3 Posts |
Posted - 2011-09-16 : 07:42:31
|
| Thank you Flameblaster and Ashishashish. I will try the query when I get to work this morning. |
 |
|
|
|