Last active
March 26, 2019 11:47
-
-
Save tsh-code/10b7e8eb6eb0335b6c92c730072f2a66 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 weights(currency, weight) AS | |
( | |
SELECT | |
DISTINCT ON(currencies)*, (random()*9+1)::decimal | |
FROM | |
( | |
SELECT unnest(array ['USD', 'PLN', 'PLN', 'PLN', 'PLN', 'EUR', 'CHR'] ) | |
) currencies | |
), | |
exchange(currency, rates) AS | |
( | |
SELECT | |
w1.currency, | |
array_agg( | |
json_build_object( | |
'currency', w2.currency, | |
'rate', round(w1.weight/w2.weight, 2) | |
) | |
) | |
FROM | |
weights w1 | |
CROSS JOIN weights w2 | |
GROUP BY | |
w1.currency | |
) | |
SELECT | |
json_agg(row_to_json(exchange,true)) | |
FROM | |
exchange |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment