๐ป ๋ฌธ์ ์ฃผ์ : Monthly Transactions I - LeetCode
Monthly Transactions I - LeetCode
Can you solve this real interview question? Monthly Transactions I - Table: Transactions +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date | date
leetcode.com
๐ป ๋ฌธ์
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.
- ์๋ณ, ๊ตญ๊ฐ๋ณ ๊ฑฐ๋ ๊ฑด์์ ์ด์ก, approved๋ ๊ฑฐ๋ ๊ฑด์์ ์ด์ก์ ๊ตฌํ๋ SQL ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์์ค. ๊ฒฐ๊ณผ ํ ์ด๋ธ ์์๋ ์๊ด ์์ต๋๋ค.
๐ป ์ฝ๋ ๋ฐ ํ์ด
select date_format(trans_date, '%Y-%m') as month,
country, count(id) as trans_count,
sum(if(state = 'approved', 1, 0)) 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' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode][SQL50] #22. Game Play Analysis IV (0) | 2024.01.02 |
---|---|
[LeetCode][SQL50] #21. Immediate Food Delivery II (1) | 2024.01.01 |
[LeetCode][SQL50] #19. Queries Quality and Percentage (0) | 2023.11.15 |
[LeetCode][SQL50] #18. Percentage of Users Attended a Contest (0) | 2023.11.15 |
[LeetCode][SQL50] #17. Project Employees I (0) | 2023.11.15 |