Compute per-coin quarterly amounts and totals
OthersData Scientist · Online AssessmentLast updated
This question evaluates competence in SQL data manipulation—specifically time-based conditional aggregation, grouping, joins, and handling entities with no activity—to compute per-coin quarterly sums and year-to-date totals.
Problem (SQL: Conditional Aggregation)
You have two tables:
coins
coin_idINT PRIMARY KEYnameVARCHAR
transactions
idBIGINT PRIMARY KEYcoin_idINT NOT NULL- Foreign key referencing
coins.coin_id dtDATETIME (assume UTC)amountDECIMAL(18,2)
Each row in transactions is a single transaction for a coin.
Task
For a given calendar year (use a parameter :year), output one row per coin with:
nameq1_amount: sum ofamountfor transactions in Q1 (Jan–Mar) of:yearq2_amount: sum ofamountfor Q2 (Apr–Jun) of:yearq3_amount: sum ofamountfor Q3 (Jul–Sep) of:yearq4_amount: sum ofamountfor Q4 (Oct–Dec) of:yeartotal_transactions: total number of transactions for that coin in:yeartotal_amount: total sum ofamountfor that coin in:year
Notes / Requirements
- Include coins with no transactions in
:year(their amounts/counts should be 0). - Output columns exactly as listed above.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.