Using TOP operator within a WHERE clause
-
I know that the TOP operator can't be within the where clause, but is there a way to achieve this behavior in a single select call? For example, if I want to select all books where publisher = 3 or 5; but I want to limit the results to only getting the top 5 for publisher 3 and the top 10 for publisher 5. Is this achievable on a single select call? Something along the lines of: select * from books where (publisher = 3 and top 5) or (publisher = 5 and top 10) Thanks in advance if anyone knows. Ron Ward
-
I know that the TOP operator can't be within the where clause, but is there a way to achieve this behavior in a single select call? For example, if I want to select all books where publisher = 3 or 5; but I want to limit the results to only getting the top 5 for publisher 3 and the top 10 for publisher 5. Is this achievable on a single select call? Something along the lines of: select * from books where (publisher = 3 and top 5) or (publisher = 5 and top 10) Thanks in advance if anyone knows. Ron Ward
You could use a union...
SELECT TOP 5 FROM books WHERE publisher = 3
UNION
SELECT TOP 5 FROM books WHERE publisher = 5 -
You could use a union...
SELECT TOP 5 FROM books WHERE publisher = 3
UNION
SELECT TOP 5 FROM books WHERE publisher = 5