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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:37:09+00:00 2026-06-03T02:37:09+00:00

I am trying to build a system, which has threads and posts. I am

  • 0

I am trying to build a system, which has threads and posts. I am trying to fetch a thread that is the most popular (the user can click “like” button to make it more popular) and has most posts. The problem is to order the results by most posts..and then by liked posts.

So for example, if I have a thread with 300 posts, and 200 likes.. while another thread has got 300 likes and 201 likes..I want the second post to be selected..

Table structure in a nutshell:

topic:
--------
topic_id
liked

comment:
-------
comment_id
topic_id

Here is my stored procedure so far:

   dbo.Trends

AS
    SELECT TOP 1 title, COUNT(com.topic_id), COUNT(topc.user_id_liked) 
FROM comment AS com
INNER JOIN topic AS topc ON com.topic_id=topc.topic_id
GROUP BY com.topic_id, topc.user_id_liked,title
ORDER BY COUNT(com.topic_id), COUNT(topc.user_id_liked) DESC

I am not sure if I am right, or will I have to result to control flow logic. I placed the topic_id from the topic table before topic liked column in the order statement..hoping the selecting/ordering of the topic_id will take precendence.

UPDATED: query updated.

  • 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-03T02:37:10+00:00Added an answer on June 3, 2026 at 2:37 am

    I don’t really know that you want. But maybe this will help:

    ;WITH CTE
    AS
    (
        SELECT
            COUNT(com.topic_id) OVER(PARTITION BY topc.liked) AS topicCount,
            COUNT(com.liked) OVER(PARTITION BY topc.topic_id) AS likedCount,
            title
        FROM 
            commnet AS com
            INNER JOIN topic AS topc 
                ON com.topic_id=topc.topic_id
    )
    SELECT TOP 1
        CTE.title,
        CTE.topicCount,
        CTE.likedCount
    FROM
        CTE
    ORDER BY
        topicCount,
        likedCount
    

    EDIT

    The differences between the GROUP BY and PARTITION BY is that PARTITION BY is an inline GROUP BY so this will not affect the number of rows. I like to use that in a CTE that is a inline view. Makes it clearer and you separate the different steps you want to do. If you remove the TOP 1 you will see what I mean.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an object that uses System.ComponentModel.ISynchronizeInvoke, which has the method:
G'day guys, trying to build a small invoicing system (that can generate PDF's using
I'm trying to build a modified role system that has the following class/relationship structure:
I'm trying to fix a very complex buildbot base build system, which has the
I am trying to build a system that uses nodejs with express to render
I'm trying to build a system which follows the repository and unit of work
I am trying to develop a system that has 2 tiers: a mobile client
I'm trying to build a GraphBot class that can answer different questions about about
I'm trying to a design for a system where the user can define their
I'm trying to build a navigation system using categories table with hierarchies. Normally, the

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.