본문 바로가기
MySql

MySQL How to calculate aggregate function SUM on an alias column?

by 베이스 공부 2021. 1. 14.
반응형

별칭 열에서 집계 함수 SUM을 계산하는 방법은 무엇입니까?

SELECT a.question_id, 
       a.level, 
       Count(a.question_id) AS rank, 
       Sum(rank)        AS total 
FROM   logs AS a, 
       question AS b 
WHERE  a.question_id = b.q_id 
       AND a.level = '2' 
GROUP  BY a.question_id 
ORDER  BY rank DESC 

 

해결 방법

 

재사용 된 별칭을 (SELECT 별칭)으로 감싸기 만하면됩니다.

SELECT a.question_id, 
       a.level, 
       COUNT(a.question_id) AS rank, 
       SUM(SELECT(rank)) AS total 
FROM   logs AS a, 
       question AS b 
WHERE  a.question_id = b.q_id 
       AND a.level = '2' 
GROUP  BY a.question_id 
ORDER  BY rank DESC 

 

참조 페이지 https://stackoverflow.com/questions/14217944

 

 

반응형

댓글