Monthly Transactions I - LeetCode
문제
Write an SQL query to find for each month and country, the number of transactions and their total amount, the number of approved transactions and their total amount.
Return the result table in any order.
풀이
SELECT SUBSTR(trans_date,1,7) AS month
,country
,COUNT(trans_date) AS trans_count
,COUNT(IF(state='approved',1,NULL)) AS approved_count
,SUM(amount) AS trans_total_amount
,SUM(IF(state='approved',amount,0)) AS approved_total_amount
FROM transactions
GROUP BY month, country'SQL > LeetCode' 카테고리의 다른 글
| [SQL] LeetCode: 184. Department Highest Salary (0) | 2023.06.08 |
|---|---|
| [SQL] LeetCode: 196. Delete Duplicate Emails (0) | 2023.06.08 |
| [SQL] LeetCode: 627. Swap Salary (0) | 2023.06.07 |
| [SQL] LeetCode: SELF JOIN 문제 (0) | 2023.05.31 |
| [SQL] LeetCode : LEFT JOIN 문제 (0) | 2023.05.31 |