Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH RankedTransactions AS (
- SELECT
- customer_id,
- amount,
- dt,
- ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY dt ASC) AS rn
- FROM
- transactions
- )
- SELECT
- c.iban,
- GROUP_CONCAT(
- CASE
- WHEN t.rn = 1 THEN t.amount
- WHEN t.amount < 0 THEN CONCAT('- ', ABS(t.amount))
- ELSE CONCAT('+ ', t.amount)
- END
- ORDER BY t.dt ASC
- SEPARATOR ' '
- ) AS transactions,
- SUM(t.amount) AS total
- FROM
- customers c
- JOIN
- RankedTransactions t ON c.id = t.customer_id
- GROUP BY
- c.iban
- ORDER BY
- total DESC;
Advertisement
Add Comment
Please, Sign In to add comment