Skip to content

Instantly share code, notes, and snippets.

@kungfuant
Created April 10, 2015 01:00
Show Gist options
  • Save kungfuant/0d8c050de8cfeba5d73b to your computer and use it in GitHub Desktop.
Save kungfuant/0d8c050de8cfeba5d73b to your computer and use it in GitHub Desktop.
WITH t AS
(SELECT
user_id,
(COALESCE(sum(cash_out - bet), 0::numeric)
+ COALESCE(sum(bonus), 0::numeric))::bigint AS gross_profit,
(COALESCE(sum(cash_out), 0::numeric)
+ COALESCE(sum(bonus), 0::numeric)
- COALESCE(sum(bet), 0::numeric))::bigint AS net_profit,
COUNT(*) AS games_played
FROM plays
GROUP BY user_id)
UPDATE users
SET gross_profit = t.gross_profit,
net_profit = t.net_profit,
games_played = t.games_played
WHERE id = t.user_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment