SQLTeam.com | Weblogs | Forums

GROUP BY error in sql

sql2008

#1

On trying to convert MySQL query to MsSql query ends in trouble.

Error states that
Column 'books.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

This code sucks
SELECT * FROM books group by books.category_id

For reference
http://sqlfiddle.com/#!3/4ed19/22

does anyone have any ideas?
Thanks in advance!


#2

you can only select those columns which you add in group by clause. you can use subquery over here.

select * from books where CategoryID = (select Categoryid from books groupby categoryid)


#3

Suppose consider 3 of books having same category_id let us say 2. How do you go with that..?