相关疑难解决方法(0)

列在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中

我的查询在没有MAX(colName)行的情况下运行正常.原始查询选择大约100列,但现在需要添加MAX(colName)列.显然,当我添加它们时,MS SQL会抱怨错误:

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

有没有办法添加这些计算值列而不必更改select中的其他100列?下面的示例已经过简化,但原始查询更大更复杂.

                SELECT 
                     g.applicationId,
                     -- (another 100 or so columns just like above)
                    -- max(g.AScore) as AScore,
                    -- max(g.APercentile) as APercentile
                FROM application a
                LEFT JOIN GREScores g ON a.applicationId = g.applicationId
                WHERE g.applicationID = 1
Run Code Online (Sandbox Code Playgroud)

谢谢

UPDATE

看起来像@OVais提到的子查询方法就可以了.如果您认为这不是一个好方法,请告诉我原因:

                SELECT 
                     g.applicationId,
                     -- (another 100 or so columns just like above)
                    (SELECT MAX(AScore) FROM GREScores WHERE GREScores.applicationId …
Run Code Online (Sandbox Code Playgroud)

sql-server

3
推荐指数
1
解决办法
3万
查看次数

标签 统计

sql-server ×1