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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:27:00+00:00 2026-06-12T07:27:00+00:00

I have 3 tables: [Users]: UserId, Name… [Topics]: TopicId, CreatorUserId… [Comments]: CommentId, TopicId… .

  • 0

I have 3 tables:

  • [Users]: UserId, Name...
  • [Topics]: TopicId, CreatorUserId...
  • [Comments]: CommentId, TopicId....

Now, I’d like to select the creators of the most popular topics according to this mechanism:
get the comments, which is newer than the specified date. Group those comments by the creator of the topic, and order by the number of rows in the groups.

However, I’m not sure that my query follows this logic(it seems according to the results, but I have only a few users and topics), but the main problem is that when I want to select the name of the user (Users.Name), I get an error:

Column ‘[TestDatabase].[dbo].[Users].[Name]’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

T-SQL query:

SELECT 
   Users.UserId, Users.Name, COUNT(Users.UserId) as RowCountInGroup
FROM [TestDatabase].[dbo].[Users]
INNER JOIN [TestDatabase].[dbo].[Topics] ON Topics.CreatorUserId = Users.UserId
INNER JOIN [TestDatabase].[dbo].[Comments] ON Comments.TopicId = Topics.TopicId
WHERE 
   Comments.CreationDate > CAST('14 SEPTEMBER 2012' as DateTime)
GROUP BY Users.UserId
ORDER BY RowCountInGroup DESC

Thanks for helping me.

EDIT: I’ve copied the original error message. I need to select the Email, the City, etc columns from the User, so I’m not sure that adding all of these to the group by clause is the best solution.

  • 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-12T07:27:01+00:00Added an answer on June 12, 2026 at 7:27 am

    You need to group by both columns (or in general: all columns) that you select (that are not aggregated by the COUNT aggregate operator):

    SELECT 
       Users.UserId, Users.Name, COUNT(Users.UserId) as RowCountInGroup
    FROM [TestDatabase].[dbo].[Users]
    INNER JOIN [TestDatabase].[dbo].[Topics] ON Topics.CreatorUserId = Users.UserId
    INNER JOIN [TestDatabase].[dbo].[Comments] ON Comments.TopicId = Topics.TopicId
    WHERE 
       Comments.CreationDate > CAST('14 SEPTEMBER 2012' as DateTime)
    GROUP BY 
       Users.UserId, Users.Name   <=== you need to add "Users.Name" here!
    ORDER BY RowCountInGroup DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following tables: users(id, name) posts (id, text, userId) comments (id, text, userId,
I have two tables like this: Table Name: users emx | userid --------------- 1
Schema: Users ID Name Password Address ID Street UserID Both tables have an ID
In short I have 2 tables: USERS: ------------------------ UserID | Name ------------------------ 0 a
I have a SQL database with two tables like this: Users Id (PK) Name
I Have the following tables Users -> UserId -> Name Roles -> RoleId ->
I have the following tables: users userId|name items itemId|userId|description What I want to achieve:
I have the following tables/views Users (View) -> UserId -> Name Roles (Table) ->
I have 2 tables Table: Users userid (int, PK) name (varchar) type (enum) Table:
I have two tables: Table Users UserId Name 1 John 2 Alice 3 Tom

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.