Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need to find number of comments on most commented post: SQL

I have two tables and i need to find the number of comments on a most commented post.
enter image description here

I can get all post ordered with a number of comments but I want to add that to subquery.

My code at the moment:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT TOP 1 p.PostID, COUNT(*) AS num_comments
FROM Comment p
GROUP BY p.PostID
ORDER BY num_comments DESC

But then I have a one column with PostID as well and I don’t want to put that on table below.
How can i get only the value of mostCommentsPerPost ?

enter image description here

Sorry If i dont explain my problem well enough, this is my first post.

Thanks!

>Solution :

just take out p.PostID from SELECT

SELECT TOP 1 COUNT(*) AS num_comments
FROM Comment p
GROUP BY p.PostID
ORDER BY num_comments DESC
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading