I am working on a query to bring back a subset of data for our business analysts to review. I received one id to test my query against but there are so many use cases I need to pull N records for them to review. What I would like to do is return a sample size of records but guarantee the single lonely id is included.
Example I generate a list of numbers. I want to take the top 5 but the number 88 must be included in the result set.
DECLARE
@startnum INT=1,
@endnum INT=100
;WITH gen AS (
SELECT @startnum AS num
UNION ALL
SELECT num+1 FROM gen WHERE num+1<=@endnum
)
SELECT * FROM gen
option (maxrecursion 100)
num
-----------
1
2
3
4
88
Is this possible?
By the way the above is from Jayvee.