人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

November 9, 2006

An error in my T-SQL

Filed under: SQL&DB Accessing

Column XXX is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
——————
http://sqlzoo.net/howto/source/z.dir/err979/sqlserver
When using a GROUP BY clause every field in the SELECT list must be either:
One of the GROUP BY terms - in this case region
An aggregate function - for example SUM or COUNT
An expression based on the above

In the example the field name may not be used on the SELECT line.
SELECT name, region, MAX(population)
FROM bbc
GROUP BY region

In a group by line each region shows up only once - however in a typical region such
as Africa there are several different name values. WHich one should SQL pick?

Solutions
-Remove the offending field from the SELECT line
-Add the field to the GROUP BY clause
-Aggregate the offending field

Specific to SQL Server

Comments »

The URI to TrackBack this entry is: http://recordsome.blogsome.com/2006/11/09/an-error-in-my-t-sql/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham