Last active
September 23, 2015 00:53
-
-
Save davisford/dcec6dfe58f3a17aa1b7 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
CREATE OR REPLACE FUNCTION assign(INT, VARCHAR(10)) | |
RETURNS INT AS $$ | |
DECLARE | |
total ALIAS FOR $1; | |
clientname ALIAS FOR $2; | |
rowcount INT; | |
BEGIN | |
UPDATE accounts | |
SET CLIENT = clientname | |
WHERE id IN ( | |
WITH nulls AS ( -- base query | |
SELECT id | |
FROM accounts | |
WHERE client IS NULL | |
), | |
randoms AS ( -- calculate random int in range 1..count(nulls.*) | |
SELECT | |
i, | |
trunc(random() * (count(*) - 1) + 1) :: INT random_value | |
FROM nulls | |
CROSS JOIN generate_series(1, total) i -- <-- total | |
GROUP BY 1 | |
), | |
row_numbers AS ( -- add row numbers to nulls in order by id | |
SELECT | |
id, | |
row_number() | |
OVER ( | |
ORDER BY id) rn | |
FROM nulls | |
) | |
SELECT id | |
FROM row_numbers, randoms | |
WHERE rn = random_value -- random row number | |
); | |
GET DIAGNOSTICS rowcount = ROW_COUNT; | |
RETURN rowcount; | |
END; | |
$$ LANGUAGE plpgsql; |
Author
davisford
commented
Sep 23, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment