Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sunnysak
Starting Member
7 Posts |
Posted - 2005-05-02 : 18:15:44
|
| Hello Guys, One more help needed please? In the following example I want to get a list of UserID pulled from the top 3 records for each in the list that has value <80Select ID from abc where (Score <80 (off latest Top 3 records of that ID))table-> abcID Value Date--------------------------A1111 0 3/30/2005 13:05A1111 80 3/9/2005 11:57A1111 40 3/4/2005 9:31A1111 80 2/23/2005 7:04A1111 86 2/17/2005 9:57A1111 86 1/28/2005 9:44A1111 80 1/20/2005 6:37A1111 83 1/6/2005 12:58A1111 80 12/3/2004 11:21ARQUE 83 12/14/2004 14:27ARQUE 67 12/14/2004 14:26ARQUE 83 12/14/2004 14:25ARQUE 83 12/14/2004 14:25AAHAM 80 2/23/2005 6:17AAHAM 33 2/4/2005 5:52AAHAM 57 1/24/2005 14:53AAHAM 80 12/2/2004 21:22AVEDO 80 4/22/2005 23:12AVEDO 64 4/7/2005 21:05AVEDO 60 3/27/2005 22:23AVEDO 80 2/7/2005 16:47AVEDO 80 1/8/2005 16:15AVEDO 80 1/8/2005 16:15ADCUL 83 3/17/2005 19:15ADCUL 80 3/3/2005 22:15AAGAR 75 3/29/2005 12:47AAGAR 80 3/4/2005 6:00AAGAR 50 2/21/2005 20:48AAGAR 75 4/22/2005 14:21---------OUTPUT-------------------so basically the output from above list should be A1111 0 3/30/2005 13:05ARQUE 67 12/14/2004 14:26AAHAM 33 2/4/2005 5:52AVEDO 60 3/27/2005 22:23AAGAR 50 2/21/2005 20:48ADCUL won't appear since he doesn't have any value <80----------------------------------------------Hopefully I explained it well. Else, let me know I'll try againThanks for your helpSunnysak |
|
|
jen
Master Smack Fu Yak Hacker
4110 Posts |
Posted - 2005-05-03 : 00:34:45
|
| use a group by on the id, select the top 3 using top, subquerying the groupingHTH--------------------keeping it simple... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
sunnysak
Starting Member
7 Posts |
Posted - 2005-05-03 : 01:32:20
|
| Ya finally I figured it out. works just the way I want it. Took a loong while, maybe I's not thinking right. Thanks for your help guys and here is my Statement for somebody who is looking.. :) SELECT * FROM <table_name_1> T WHERE (id IN (SELECT TOP 3 id FROM <table_name_1> T1 ORDER BY <any_field1>, <date_field> DESC))ORDER BY <anyfield1> |
 |
|
|
|
|
|