T-SQL Query: Select Middle Record
SELECT TOP 1 query in T-SQL helps to find the first or the last record of the table data sorted by some criteria. But what if we need to find exactly middle record entry in the table ? Below is a small T-SQL query snippet that demonstrates a technique how to get middle record in a single query (pay attention to the inline view used):
SELECT TOP 1 UserId FROM (SELECT TOP 50 PERCENT UserId FROM T_USERS WITH (NOLOCK) ORDER BY UserId ASC) AS T1 ORDER BY 1 DESC
Saturday, July 9, 2005 1:50 AM