Study - Problems(IT)/LeetCode - SQL

1193.Monthly Transactions

Dev.D 2024. 11. 16. 18:59

* Probelm

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.

 

* Solution

# Write your MySQL query statement below

Select DATE_FORMAT(trans_date, '%Y-%m') AS month,
            country,
            COUNT(*) AS trans_count,
            sum(state='approved') as approved_count,
            SUM(amount) AS trans_total_amount,
            SUM(IF(state = 'approved',amount,0)) AS approved_total_amount
    FROM Transactions
    GROUP BY 1,2;

'Study - Problems(IT) > LeetCode - SQL' 카테고리의 다른 글

550. Game Play Analysis IV  (0) 2024.11.19
1174.Immediate Food Delivery2  (2) 2024.11.17
1211.Queries Quality and Percentage  (0) 2024.11.15
1633. Percentage of Users Attended a Contest  (1) 2024.11.14
1075. Project Employees I  (1) 2024.11.10