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 486117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:28:14+00:00 2026-05-13T01:28:14+00:00

Consider a table like tbl_ranks ——————————– family_id | item_id | view_count ——————————– 1 10

  • 0

Consider a table like

tbl_ranks
--------------------------------
family_id | item_id | view_count 
--------------------------------
1           10        101
1           11        112
1           13        109

2           21        101
2           22        112
2           23        109

3           30        101
3           31        112
3           33        109

4           40        101
4           51        112
4           63        109

5           80        101
5           81        112
5           88        109

I need to generate a result set with the top two(2) rows for a subset of family ids (say, 1,2,3 and 4) ordered by view count.
I’d like to do something like

select top 2 * from tbl_ranks where family_id = 1 order by view_count
union all
select top 2 * from tbl_ranks where family_id = 2 order by view_count
union all
select top 2 * from tbl_ranks where family_id = 3 order by view_count
union all
select top 2 * from tbl_ranks where family_id = 4 order by view_count

but, of course, order by isn’t valid in a union all context in this manner. Any suggestions? I know I could run a set of 4 queries, store the results into a temp table and select the contents of that temp as the final result, but I’d rather avoid using a temp table if possible.

Note: in the real app, the number of records per family id is indeterminate, and the view_counts are also not fixed as they appear in the above example.

  • 1 1 Answer
  • 2 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-05-13T01:28:15+00:00Added an answer on May 13, 2026 at 1:28 am

    You can try something like this

    DECLARE @tbl_ranks TABLE(
            family_id INT,
            item_id INT,
            view_count INT
    )
    
    INSERT INTO @tbl_ranks SELECT 1,10,101
    INSERT INTO @tbl_ranks SELECT 1,11,112
    INSERT INTO @tbl_ranks SELECT 1,13,109
    
    INSERT INTO @tbl_ranks SELECT 2,21,101
    INSERT INTO @tbl_ranks SELECT 2,22,112
    INSERT INTO @tbl_ranks SELECT 2,23,109
    
    INSERT INTO @tbl_ranks SELECT 3,30,101
    INSERT INTO @tbl_ranks SELECT 3,31,112
    INSERT INTO @tbl_ranks SELECT 3,33,109
    
    INSERT INTO @tbl_ranks SELECT 4,40,101
    INSERT INTO @tbl_ranks SELECT 4,51,112
    INSERT INTO @tbl_ranks SELECT 4,63,109
    
    INSERT INTO @tbl_ranks SELECT 5,80,101
    INSERT INTO @tbl_ranks SELECT 5,81,112
    INSERT INTO @tbl_ranks SELECT 5,88,109
    
    SELECT  *
    FROm    (
                SELECT  *,
                        ROW_NUMBER() OVER(PARTITION BY family_id ORDER BY view_count DESC) MyOrder
                FROM    @tbl_ranks
            ) MyOrders
    WHERE   MyOrder <= 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider a table or CTE structured like this: Name Num ---- ---- Abc 12
I have a table like below Consider the table id=testtable, and there is no
Consider a table datatbl like this: +----------+ | strfield | +----------+ | abcde |
Consider the following: Table - id, parentid What I'd like to do, is I'd
please consider this image: I have a table like this: Age Active Men/Women -------------------------------------------------
Consider a table with three rows with heights 10, *, 10. I'd like the
Consider a table like this: | Name | Version | Other | | ---------------------|-------|
Consider a table like this: folders_table ----------------------- INT id_folder INT id_folder_parent VARCHAR folder_name Which
Consider a simple table with an auto-increment column like this: CREATE TABLE foo (
I'd like to know when you should consider using multiple table in your query

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.