개발자의 끄적끄적

[mssql] invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause 에러 본문

개발/sql

[mssql] invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause 에러

효벨 2020. 2. 22. 03:00
728x90
반응형

[mssql] invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause 에러

SELECT TOP 3 MAX(share.id) as share_id, share.user_id 
FROM share  
WHERE share.post_id = 5468   
GROUP BY share.user_id 
ORDER BY share.id desc

 

위와같은 쿼리가 있다고 가정하고

실행하면 order by 에러가 뜹니다.

 

그러면 아래와같이 order by 필드를 MAX ( ) 로 감싸서 실행하면 해결됩니다!!

 

SELECT TOP 3 MAX(share.id) as share_id, share.user_id 
FROM share  
WHERE share.post_id = 5468   
GROUP BY share.user_id 
ORDER BY MAX(share.id) desc

 

참고들하세용!!!

 

참고 사이트 : https://stackoverflow.com/questions/33580104/invalid-in-the-order-by-clause-because-it-is-not-contained-in-either-an-aggregat

반응형
Comments