SQLTeam.com | Weblogs | Forums

Retrieve last record


#1

SELECT SportId,OddsType,ListName,Sequence
FROM [dbo].[tbl_Suggestions] TS
INNER JOIN dbo.tbl_OddsTypes OT ON TS.OddsType=OT.OddsTypeId
WHERE MatchId=@MatchId AND OddsType IS NOT NULL AND SportId IS NOT NULL AND SportId=1 and OddsType='10'
Group By SportId,OddsType,ListName,Sequence
ORDER BY Sequence

Result:
9 1 10 1 1.70 4295500 2016-06-14 22:15:00.000 9210897 1449 NULL
9 X 10 2 3.25 4295501 2016-06-14 22:15:00.000 9210897 1449 NULL
9 2 10 3 4.80 4295502 2016-06-14 22:15:00.000 9210897 1449 NULL
9 1 10 1 1.65 4308119 2016-06-14 22:15:00.000 9210897 1449 NULL
9 X 10 2 3.25 4308123 2016-06-14 22:15:00.000 9210897 1449 NULL
9 2 10 3 4.95 4308127 2016-06-14 22:15:00.000 9210897 1449 NULL
9 1 10 1 1.65 4312769 2016-06-14 22:15:00.000 9210897 1449 NULL
9 X 10 2 3.30 4312771 2016-06-14 22:15:00.000 9210897 1449 NULL
9 2 10 3 5.00 4312773 2016-06-14 22:15:00.000 9210897 1449 NULL

I have
1
X
2
1
X
2...

How can I retrieved last record in this case:
9 1 10 1 1.65 4312769 2016-06-14 22:15:00.000 9210897 1449 NULL
9 X 10 2 3.30 4312771 2016-06-14 22:15:00.000 9210897 1449 NULL
9 2 10 3 5.00 4312773 2016-06-14 22:15:00.000 9210897 1449 NULL

If i have

test1
test2
test3
test4
test1
test2
test3
test4

I need to get test1 test2 test3 test4 which is bold.

Please help


#2

I am afraid I don't quite follow the logic you are trying to implement. Are you trying to get unique records? You could use DISTINCT keyword if that is what you are trying to accomplish. However, looking at your larger example, it doesn't seem like that is what you are after. What is the rule you used to pick only three rows out of the nine rows in the first example?