My query below runs fine without the MAX(colName) lines. The original query selects about 100 columns, but now the MAX(colName) columns need to be added. Obviously when I add them, MS SQL complains with the error:
"Column 'applicationId' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause"
Is there any way to add these computed value columns without having to change the other 100 columns in the select?? The example below is simplified but the original query is a lot bigger and more complex.
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
Thanks
UPDATE
Looks like the subquery approach mentioned by @OVais did the trick. If you believe this is not a good approach, please tell me why:
SELECT
g.applicationId,
-- (another 100 or so columns just like above)
(SELECT MAX(AScore) FROM GREScores WHERE GREScores.applicationId = a.applicationId) AS tAScore
-- max(g.APercentile) as APercentile
FROM application a
LEFT JOIN GREScores g ON a.applicationId = g.applicationId
WHERE g.applicationID = 1
You can use subquery instead of using aggregate function..
select col1,col2,(select Sum(col3) from table_name) as Sum from table_name