Monthly Transactions I — LeetCode 1193 Python Solution
MediumDatabase
- Problem
- #1193
- Reading time
- 3 min
- Source
- leetcode.com
Table schema
SQL
Table: Transactions +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | country | varchar | | state | enum | | amount | int | | trans_date | date | +---------------+---------+ id is the primary key of this table. The table has information about incoming transactions.Example
SQL
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| id | int |
| country | varchar |
| state | enum |
| amount | int |
| trans_date | date |
+---------------+---------+
id is the primary key of this table.
The table has information about incoming transactions.
The state column is an enum of type ["approved", "declined"].Python solution
Python
import duckdb
import pandas as pd
def solution(transactions: pd.DataFrame) -> pd.DataFrame:
con = duckdb.connect()
con.register("Transactions", transactions)
return con.execute("""SELECT
DATE_FORMAT(trans_date, '%Y-%m') AS month,
country,
COUNT(1) 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;""").df()Complexity
| Measure | Complexity |
|---|---|
| Time | O(n log n) (typical) |
| Space | O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 1193. Monthly Transactions I?
- LeetCode 1193. Monthly Transactions I is rated Medium on LeetCode.
- What topics does LeetCode 1193. Monthly Transactions I cover?
- LeetCode 1193. Monthly Transactions I is tagged Database on LeetCode.