Created
October 3, 2014 10:59
-
-
Save sergio1990/df4b3a42438a3ffb300e to your computer and use it in GitHub Desktop.
Calculating bet profit using custom postgresql function
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
CREATE OR REPLACE FUNCTION bet_profit(state character varying, stake float, odds float) RETURNS float AS $$ | |
DECLARE | |
result float := 0; | |
half_stake float :=0; | |
BEGIN | |
CASE state | |
WHEN 'in_process' THEN | |
result := 0; | |
WHEN 'guessed' THEN | |
result := (stake * odds); | |
WHEN 'losed' THEN | |
result := 0; | |
WHEN 'half_losed' THEN | |
result := (stake / 2.0); | |
WHEN 'half_winnered' THEN | |
half_stake := stake / 2.0; | |
result := (half_stake * odds) + half_stake; | |
END CASE; | |
RETURN result - stake; | |
END; | |
$$ LANGUAGE plpgsql; | |
SELECT bet_profit(state, (details->'stake')::float, (details->'odds')::float) AS profit, id FROM bets LIMIT 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment