invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause


Column 'dbo.tbm_vie_View.ViewID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

https://stackoverflow.com/questions/13999817/reason-for-column-is-invalid-in-the-select-list-because-it-is-not-contained-in-e

Suppose I have the following table T:

a b -------- 1 abc 1 def 1 ghi 2 jkl 2 mno 2 pqr

And I do the following query:

SELECT a, b FROM T GROUP BY a

The output should have two rows, one row where a=1 and a second row where a=2.

But what should the value of b show on each of these two rows? There are three possibilities in each case, and nothing in the query makes it clear which value to choose for b in each group. It's ambiguous.

This demonstrates the single-value rule, which prohibits the undefined results you get when you run a GROUP BY query, and you include any columns in the select-list that are neither part of the grouping criteria, nor appear in aggregate functions (SUM, MIN, MAX, etc.).

Fixing it might look like this:

SELECT a, MAX(b) AS x FROM T GROUP BY a

Now it's clear that you want the following result:

a x -------- 1 ghi 2 pqr

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM