Leetcode - 1193. Monthly Transactions I

Indo Yoon

Problem

Monthly Transactions I - LeetCode
Monthly Transactions I - Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

Solution

SELECT 
    DATE_FORMAT(trans_date, "%Y-%m") AS month,
    country, 
    COUNT(id) AS trans_count, 
    SUM(state = 'approved') AS approved_count, 
    SUM(amount) AS trans_total_amount, 
    SUM(CASE 
            WHEN state = 'approved' THEN amount 
            ELSE 0 
        END) AS approved_total_amount
FROM Transactions
GROUP BY month, country