sudama_bera

Untitled

Oct 28th, 2026
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.64 KB | Source Code | 0 0
  1. WITH RankedTransactions AS (
  2.     SELECT
  3.         customer_id,
  4.         amount,
  5.         dt,
  6.         ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY dt ASC) AS rn
  7.     FROM
  8.         transactions
  9. )
  10. SELECT
  11.     c.iban,
  12.     GROUP_CONCAT(
  13.         CASE
  14.             WHEN t.rn = 1 THEN t.amount
  15.             WHEN t.amount < 0 THEN CONCAT('- ', ABS(t.amount))
  16.             ELSE CONCAT('+ ', t.amount)
  17.         END
  18.         ORDER BY t.dt ASC
  19.         SEPARATOR ' '
  20.     ) AS transactions,
  21.     SUM(t.amount) AS total
  22. FROM
  23.     customers c
  24. JOIN
  25.     RankedTransactions t ON c.id = t.customer_id
  26. GROUP BY
  27.     c.iban
  28. ORDER BY
  29.     total DESC;
Advertisement
Add Comment
Please, Sign In to add comment