Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Last active March 26, 2019 11:47
Show Gist options
  • Save tsh-code/10b7e8eb6eb0335b6c92c730072f2a66 to your computer and use it in GitHub Desktop.
Save tsh-code/10b7e8eb6eb0335b6c92c730072f2a66 to your computer and use it in GitHub Desktop.
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