FernandoLorival
Starting Member
19 Posts |
Posted - 2011-03-30 : 15:07:49
|
Hi all,I came across with a problem that I can't find a solution for, hope someone here can help.I need to display the top 3 values for all the players in a table that has more then 3 records per player.Here's an example:--creates the example tableCREATE TABLE #temp ( ID int, Name char(30), Value int )--Inserts values insert into #temp(id,name,value) values (1,'F1',14)insert into #temp(id,name,value) values (2,'F1',18)insert into #temp(id,name,value) values (3,'F1',12)insert into #temp(id,name,value) values (4,'F1',5)insert into #temp(id,name,value) values (5,'F1',7)insert into #temp(id,name,value) values (6,'F1',15)insert into #temp(id,name,value) values (7,'F2',12)insert into #temp(id,name,value) values (8,'F2',4)insert into #temp(id,name,value) values (9,'F2',8)insert into #temp(id,name,value) values (10,'F2',12)insert into #temp(id,name,value) values (11,'F3',15)insert into #temp(id,name,value) values (12,'F3',7)insert into #temp(id,name,value) values (13,'F3',5)insert into #temp(id,name,value) values (14,'F3',12)insert into #temp(id,name,value) values (15,'F3',11)insert into #temp(id,name,value) values (16,'F3',8)-- shows valuesselect * from #tempAs you can see from all the lines I just need the top 3...The closest thing I got was with a UNION statement, something like:select top 3 * from #temp WHERE NAME = 'F1'UNIONselect top 3 * from #temp WHERE NAME = 'F2'UNIONselect top 3 * from #temp WHERE NAME = 'F3'but that will not work because I dont know all the NAMES for the WHERE CLAUSE and also the values are not in the correct order (SORT)My end result needs to be something like this:ID Name Value----------- ------------------------------ -----------2 F1 186 F1 151 F1 147 F2 1210 F2 129 F2 811 F3 1514 F3 1215 F3 11Thanks in advance for all your help. |
|