Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8634445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:43:36+00:00 2026-06-12T09:43:36+00:00

I have a database with a table book ( id, name, display, priority )

  • 0

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:

  1. when I try to select top 3 order by priority then 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.

  2. 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:43:38+00:00Added an answer on June 12, 2026 at 9:43 am

    try this:

    with cte as (
         select *,row_number() over(partition by priority  order by id) as row_num
         from book
         where  display='true'
         and priority in (1,2,3)
         )
    select id,name,display,priority
    from   cte 
    where  row_num=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm having a problem with my database. I have the folowing: create table book
For example, in my database I have 3 tables, the Books table for storing
I have a table [Book] in a database that has a [AuthorId] and [Description]
i'm having a problem with my database. I have the folowing: create table book
i have database table like this +-------+--------------+----------+ | id | ip | date |
that my problem: I have database table like that: id (AI) market_id 1 6
I have a database table with some rows that I want to fetch using
I have a database table containing dates (`date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00').
I have a database table, my goal is to read in each of the
I have a database table named Categories . I want to create a form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.