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?
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.
In 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:
(Note that I changed the
in (select ...)into aninner join.)You might want to add an
order byalso, to specify which five projects you want data from.