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