I’m fairly new to using MSSQL and have run into a weird problem. Given the first record id from the last query, I need to find the 50 rows before that that meet the same criteria as the first query and return them in descending order. This is for an ASP.NET site, and I’m pretty sure the ASP side is correct but it gives the wrong results when I try to run the SQL query
This is the SQL I’ve been trying
WITH PreviousPosts AS
(
SELECT *
FROM [database].[dbo].[table]
WHERE (A_1STNAME LIKE '(variable)%')
AND A_RECID < '(lastPost)'
)
SELECT TOP 50 * FROM PreviousPosts
ORDER BY A_RECID ASC
where “(variable)” and “(lastPost)” are actually filled in with the correct parameters with my C#
I’m no longer receiving errors but now I’m getting the same results no matter what is provided as the lastPost.
Update: The results are the first 50 in the table that have firstnames like (in my test case) “James”
Any help is appreciated 🙂
Here’s an attempt, with what I know of your schema now. I still had to make more assumptions than I’m comfortable with, so this might not be right as is: