๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
SQL/LeetCode

[LeetCode][SQL50] #20. Monthly Transactions I

by ์ด๋ฎด 2023. 11. 16.
728x90

๐Ÿ’ป ๋ฌธ์ œ

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
 
 

 

728x90
๋ฐ˜์‘ํ˜•