SQL Server GROUP BY error
NickName:Thomas R Ask DateTime:2009-07-14T06:27:28

SQL Server GROUP BY error

Here is the query I am trying to run:

  SELECT TOP 5 PageComment.ID
    FROM PageComment
   WHERE PageComment.ParentID IN (SELECT ID FROM ProjectPage)
GROUP BY PageComment.ParentID

What I want to get as a result, is one comment per project however, this query gives this error:

"[Microsoft][SQL Native Client][SQL Server] Column PageComment.ID is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"

I understand what it is saying, but this query would work in MySQL, how would I achieve this in SQL Server?

Copyright Notice:Content Author:「Thomas R」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/1122436/sql-server-group-by-error

Answers
Adrian Godong 2009-07-13T22:29:59

You have to put all columns you are selecting into the GROUP BY clause.\n\nFor example:\n\nSELECT TOP 5 PageComment.ID\nFROM PageComment\nWHERE PageComment.ParentID IN (SELECT ID FROM ProjectPage)\nGROUP BY PageComment.ID\n",


borjab 2009-07-13T22:32:21

If you know that each parentID has just one ID you can make:\n\nSELECT TOP 5 \n MAX(PageComment.ID )\n from PageComment \n WHERE PageComment.ParentID IN (SELECT ID FROM ProjectPage) \nGROUP BY PageComment.ParentID\n",


Nippysaurus 2009-07-13T22:59:44

Run the following query and view the results ...\n\nSELECT TOP 5 PageComment.ID, PageComment.ParentID\nFROM PageComment\nWHERE PageComment.ParentID IN (SELECT ID FROM ProjectPage)\nGROUP BY PageComment.ParentID\n\n\nThen have a think about how grouping would work on that data. We cannot help you with the grouping because we do not know what you are trying to do.",


Guffa 2009-07-13T22:55:46

Yes, MySQL would let you do that, and it would return what you ask for. The problem with that is that what you ask for is unspecific, so the result is equally unspecific. It will return one of the PageComment.ID values from each group, but it won't care which one.\n\nIn SQL Server you have to be more specific and tell exactly which PageComment.ID value it is that you want from each group. For example the one with the lowest value:\n\nselect top 5 min(c.ID)\nfrom PageComment c\ninner join ProjectPage p on p.ID = c.ParentID\ngroup by c.ParentID\n\n\n(Note that I changed the in (select ...) into an inner join.)\n\nYou might want to add an order by also, to specify which five projects you want data from.",


Remus Rusanu 2009-07-13T22:31:30

What exactly are you GROUPING BY for? You don't have any AGGREGATE function in your query (like SUM, COUNT, MAX, MIN etc) so there is no need for GROUP BY. Do you want to ORDER BY by any chance?",


More about “SQL Server GROUP BY error” related questions

SQL Server : GROUP error

I have a problem in SQL Server. I have a table name rTable and inside many columns of different types (Date, XML, varchar and so..) Now I need group by one of this columns. SELECT TOP 100 * FROM ...

Show Detail

SQL Server GROUP BY error

Here is the query I am trying to run: SELECT TOP 5 PageComment.ID FROM PageComment WHERE PageComment.ParentID IN (SELECT ID FROM ProjectPage) GROUP BY PageComment.ParentID What I want to...

Show Detail

GROUP BY error in SQL Server

When I delete Group By clause it works. The main problem is that the query gets repeated rows, so I added Group By to clear the results. SELECT TOP 5 O.ItemCode, O.ItemName FROM OITM O J...

Show Detail

Microsoft SQL Server: Error with Group By

I'm new to Microsoft SQL Server 2014. I run this SQL code: SELECT TOP(10) 'DBSG' as seek_entity, * FROM DBSG..PM00200 and get this result: Next, I want to find out total line items for that entity

Show Detail

Group by is throwing error in SQL Server

I want to group by service name to all my record and I am using this query in SQL Server but it's throwing an error select max(c.service_id) as service_id, a.ser_id, b.UserID, SQRT(POWER(69....

Show Detail

Each GROUP BY expression error in sql server

I am building a stored procedure in MS SQL SERVER 2012. I want to display average level of current day. CREATE PROCEDURE [dbo].[AverageP] @UserID INT AS BEGIN SELECT DAY(GETDATE()), AVG(Le...

Show Detail

Group by * in SQL Server

I have a table, say, A with 100 columns. I am trying to join another table B to this, which has certain measures which need to be aggregated to the level of the first table, which means I need to g...

Show Detail

Group by working in SQL Server 2000 but not in SQL Server 2005

I used following query in SQL Server 2000 SELECT U.FirstName, SUM(VE.Score)AS Score, SUM(VE.QuizTime) AS Time, SUM(VE.IsQuizType) AS QuizesAttempted, SUM(VE.IsProgrammingType) AS

Show Detail

UNION and GROUP BY in SQL server

I need help for create a View in SQL Server. I have some thing like this SELECT MemberId, Name, SUM(TotalSales) as SalesAmount, CreatedOn FROM ( ...

Show Detail

Cannot connect to SQL Server as a member of AD group

I am facing an error to connect to SQL Server as a member of a new AD group. Could anyone advise below? I am a member of an AD group and this AD group has DB owner access in the instance and datab...

Show Detail