[SQL] 특정 조건의 sum을 select에 포함하고 싶을 때

2021. 10. 7. 09:47개발공부/DB

특정 조건의 sum을 select에 포함하고 싶을 때

select w.*,
       sum(case when h.type in ('MEMBER_10', 'MEMBER_20') then amount end + case when h.type in ('MEMBER_10', 'MEMBER_20') then tax end) total
from Wallet w
         left join Member m on w.memberId = m.id
         left join Wallet h on h.walletId = w.id
where m.type='MEMBER_ADMIN'
group by w.id;

이런 식으로 case when을 붙여서 사용할 수 있습니다.


참고자료: stackoverflow

'개발공부 > DB' 카테고리의 다른 글

Transaction  (11) 2022.06.04
[MySql] mysql timestamp 서버 UTC 타임존  (0) 2021.10.27
[JPA] 복합 식별키를 update 해야할 때  (0) 2021.10.20
(1) MariaDB 로컬 사용자 추가  (0) 2021.06.08