Created
April 10, 2015 01:00
-
-
Save kungfuant/0d8c050de8cfeba5d73b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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