본문 바로가기

SQL/LeetCode

[SQL] LeetCode : 1193. Monthly Transactions I

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