I’ve got the following working query:
string sqlString =
"SELECT * " +
"FROM (SELECT ROW_NUMBER() OVER (ORDER BY Id DESC) AS RowNum, * " +
"FROM StreamView " +
"WHERE Recipient = @Recipient " +
") AS RowConstrainedResult " +
"WHERE RowNum >= @startAt " +
"AND RowNum < @howMany " +
"ORDER BY RowNum;";
Which then returns the proper rows given a startAt and howMany variables.
I would like to do the same with the query below:
string sqlString =
"SELECT DISTINCT l.* FROM Streams l " +
"INNER JOIN Friendships f ON f.Sender = @UserName OR f.Recipient = @UserName " +
"WHERE l.Sender <> @UserName AND l.Recipient <> @UserName AND ( " +
"l.Sender = f.Recipient OR l.Sender = f.Sender OR " +
"l.Recipient = f.Sender OR l.Recipient = f.Recipient) " +
"ORDER BY DateTime DESC;";
The query above works perfectly, but i’d like to get ranges instead of all the available rows. I need the same functionality of the first query.
Ideas? thanks.
You should be able to put your original query in a subquery or a CTE and then select against that with a ROW_NUMBER() call. For example, something like (untested):
I don’t know what kind of a query plan SQL Server would use for this though, so performance might not be very good.