I have a database with a table book (id, name, display, priority)
For example:
________________________________________________________
| id | name | display | priority |
--------------------------------------------------------
| 1 | test1 | True | 5 |
| 2 | test2 | True | 3 |
| 3 | test3 | False | 4 |
| 4 | test4 | True | 1 |
| 5 | test5 | True | 2 |
| 6 | test6 | False | 1 |
| 7 | test7 | True | 1 |
| 8 | test8 | True | 4 |
| 9 | test9 | True | 3 |
| 10 | test10 | False | 2 |
| 11 | test11 | True | 3 |
| 12 | test12 | True | 5 |
--------------------------------------------------------
I need to write a query to get only 3 rows with priority 1 and 2 and 3 and display is true
I use this stored procedure to display books that have priority 1 and 2 and 3 (the most specific books)
Problem:
-
when I try to
select top 3 order by prioritythen the query get the first 3 rows with priority = 1, while I need 1-record with priority = 1 and 1-record with priority = 2 and 1-record with priority = 3. -
when I try to get
distinct book, distinct works over all records not on priority only
Result:
I need the result to be like:
________________________________________________________
| id | name | display | priority |
--------------------------------------------------------
| 4 | test4 | True | 1 |
| 2 | test2 | True | 3 |
| 5 | test5 | True | 2 |
--------------------------------------------------------
How can I do that?
thanks for help
try this: