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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:58:29+00:00 2026-05-13T16:58:29+00:00

Currently I have 2 table [Category] -> PK| CAT_ID -> CAT_PARENT (link to itself

  • 0

Currently I have 2 table

[Category] -> PK| CAT_ID -> CAT_PARENT (link to itself | if it's a top parent category then it's 0)
[Posts] -> PK | POST_ID -> FK | CAT_ID | CREATE_DATE

How do I select top 15 ROWS of Posts in every CAT_PARENT which have multiple child category. So the total Posts in CAT_PARENT and all it’s child are only 15. we have multiple CAT_PARENT here. so we can return multiple group of post which contain 15 post for every CAT_PARENT and it’s child category

The problem here is to have it in one round trip of query to the SQL server since one query can take up to 200 posts so it could be the best if you can write it in 1 T-sQL query / linq query.

I prefer if you can write it in LINQ. But it’s also OK to write it in T-SQL.

Thank you very much 🙂


Thanks to Alexander’s solution down here, i modified some part and it work well with 186 query and some lazy loaded column for less than 2s (remotely) to my SQL server

ALTER procedure [dbo].[get_topParentPost] (
    @quantity int
)
as
    WITH t AS (
    SELECT ROW_NUMBER() OVER (PARTITION BY top_level.CAT_ID ORDER BY p.CREATE_DATE DESC) AS row_num, 
    top_level.CAT_ID AS top_level_cat_id, child_category.CAT_ID AS category_id, p.POST_ID, p.CREATE_DATE, p.VALIDATE,
    p.CAT_ID, p.DESCRIPTION, p.DRAF_OF, p.END_DATE, p.MOD_DATE, p.ON_HOMEPAGE, p.PUBLISH_DATE, p.[STATE], p.THUMB_ID, p.TITLE, p.[TYPE],
    p.[VIEW]
    FROM 
        (SELECT cat_id, 0 as cat_parent FROM Categories c WHERE CAT_PARRENT = 0) AS top_level
        INNER JOIN Categories AS child_category  
          ON child_category.CAT_PARRENT = top_level.CAT_ID OR child_category.CAT_ID = top_level.CAT_ID
        INNER JOIN Posts p 
          ON child_category.CAT_ID = p.CAT_ID AND p.VALIDATE = 1
    )
    SELECT * FROM t WHERE row_num <= @quantity

I modified some part which helps the query select top 15 according to descending date instead of ID ascending

  • 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-05-13T16:58:29+00:00Added an answer on May 13, 2026 at 4:58 pm

    If it’s for SQL Server 2005 or later, you can do:

    SELECT t.top_level_cat_id, t.category_id, t.post_id, t.post_date 
    FROM (
        SELECT ROW_NUMBER() OVER (PARTITION BY top_level.cat_id ORDER BY p.post_id) AS row_num, 
            top_level.cat_id AS top_level_cat_id, 
            child_category.cat_id AS category_id, 
            p.post_id, 
            p.post_date
        FROM 
            Post p
            INNER JOIN Category AS child_category 
            ON child_category.cat_id = p.cat_id  
            INNER JOIN Category AS top_level
            ON top_level.cat_id = child_category.cat_parent
    ) AS t 
    WHERE t.row_num <= 15
    

    Here’s T-SQL I used for creating test tables:

    CREATE TABLE Category (cat_id INT, cat_parent INT);
    --top level
    INSERT INTO Category VALUES(1, 0);
    INSERT INTO Category VALUES(2, 0);
    -- child categories
    INSERT INTO Category VALUES(3, 1);
    INSERT INTO Category VALUES(4, 1);
    INSERT INTO Category VALUES(5, 2);
    
    CREATE TABLE Post(post_id INT, cat_id INT, post_date DATETIME);
    INSERT INTO Post VALUES(1, 3, GETDATE());
    INSERT INTO Post VALUES(2, 3, GETDATE());
    INSERT INTO Post VALUES(3, 3, GETDATE());
    INSERT INTO Post VALUES(4, 3, GETDATE());
    INSERT INTO Post VALUES(5, 3, GETDATE());
    INSERT INTO Post VALUES(6, 3, GETDATE());
    INSERT INTO Post VALUES(7, 3, GETDATE());
    INSERT INTO Post VALUES(8, 3, GETDATE());
    INSERT INTO Post VALUES(9, 3, GETDATE());
    INSERT INTO Post VALUES(10, 3, GETDATE());
    INSERT INTO Post VALUES(11, 3, GETDATE());
    INSERT INTO Post VALUES(12, 3, GETDATE());
    INSERT INTO Post VALUES(13, 3, GETDATE());
    INSERT INTO Post VALUES(14, 3, GETDATE());
    INSERT INTO Post VALUES(15, 3, GETDATE());
    -- these records won't appear 
    INSERT INTO Post VALUES(16, 3, GETDATE());
    INSERT INTO Post VALUES(17, 4, GETDATE());
    INSERT INTO Post VALUES(18, 4, GETDATE());
    
    INSERT INTO Post VALUES(19, 5, GETDATE());
    INSERT INTO Post VALUES(20, 5, GETDATE());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I jump straight into the problem, currently I have a table as such id
I currently have an option that allows a user to change the display order
Let's assume I have 3 tables in a MySQL table with the following fields.
I have a classifieds website, and I am thinking about redesigning the database a
Building an application to calculate metrics on Service Level Agreements. I have access to
TABLE PRODUCTS_CATEGORIES product_id category_id link_type position 2 22 M 0 3 22 M 0
I am using a custom Router to enable pages like: mytutorialsite.com/category/:categoryname # added to
I make LINQ query for the Books sample database: http://www.codeproject.com/KB/linq/linqtutorial.aspx Sorry for external link,
If I have the query below which selects a forum board, but the board
I wonder if there is any practice when designing a forum. I want to

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.