I have a View that produces a one-to-many result list. What I need it to do is limit the "many" part to 5 instead of listing every possible result. Here's my query:
And these are the results I get:
Roommate1 Potential_Roommate2
AAB0001 ABB0001
AAB0001 ACL0001
AAB0001 ACM0003
AAB0001 ADS0001
AAB0001 AEB0002
AAB0001 AEC0001
AAB0001 AED0001
AAB0001 AEE0001
AAB0001 AEH0001
AAB0001 AEV0001
. . . . <snip> hundres of records
AKB0001 ces0004
AKB0001 cgh0001
AKB0001 CHs0001
AKB0001 CJK0002
AKB0001 CJM0002
AKB0001 ckf0001
AKB0001 CKZ0001
AKB0001 cln0001
AKB0001 CLY0001
. . . . . <snip> hundreds more
What I want to do is limit the results for Roommate1 to the top 5 instead of every single one.
Code:
SELECT TOP 100 percent a.GID AS Roommate1, b.GID AS Potential_Roommate2
FROM dbo.APPLICATION a INNER JOIN
dbo.APPLICATION b ON a.MATCH1 = b.MATCH1 AND a.MATCH2 = b.MATCH2 AND a.MATCH3 = b.MATCH3 AND a.GID <> b.GID INNER JOIN
dbo.ADMISSIONS c ON a.GID = c.GID INNER JOIN
dbo.ADMISSIONS d ON b.GID = d.GID AND c.GENDER = d.GENDER
WHERE (NOT (a.PRIORITY_DATE IS NULL)) AND (NOT (b.PRIORITY_DATE IS NULL))
ORDER BY a.GID, b.GID
Roommate1 Potential_Roommate2
AAB0001 ABB0001
AAB0001 ACL0001
AAB0001 ACM0003
AAB0001 ADS0001
AAB0001 AEB0002
AAB0001 AEC0001
AAB0001 AED0001
AAB0001 AEE0001
AAB0001 AEH0001
AAB0001 AEV0001
. . . . <snip> hundres of records
AKB0001 ces0004
AKB0001 cgh0001
AKB0001 CHs0001
AKB0001 CJK0002
AKB0001 CJM0002
AKB0001 ckf0001
AKB0001 CKZ0001
AKB0001 cln0001
AKB0001 CLY0001
. . . . . <snip> hundreds more
What I want to do is limit the results for Roommate1 to the top 5 instead of every single one.