Please write an SQL statement that returns all Users, all Balance transactions (in this…
- User
Please write an SQL statement that returns all Users, all Balance transactions (in this task please ignore Currencies that do not have a key in the Currency table) with currency name and calculated value of the currency in USD for the next day. Below is a table of result columns and the corresponding calculation formula. Output Column Formula (pseudocode) name source: user.name if user.name is NULL then return not defined value lastname source: user.lastname if user.lastname is NULL then return not defined value currency_name source: currency.name currency_in_usd involved sources: currency.rate_to_usd, currency.updated, balance.updated.Take a look at a graphical interpretation of the formula below.
You need to find a nearest rate_to_usd of currency in the past (t1). If t1 is empty (means no rates in the past), then find a nearest rate_to_usd of currency in the future (t2). Use t1 OR t2 rate to calculate a currency in USD format.
See a sample of the output below. Sort the result by User Name in descending order and then by User Lastname and Currency name in ascending order.
Написал примерно скрипт, исправь ошибки, чтоб всё работало правильно -- insert into currency values (100, 'EUR', 0.85, '2022-01-01 13:29'); -- insert into currency values (100, 'EUR', 0.79, '2022-01-08 13:29'); WITH cte_nearest_rate AS ( SELECT b.user_id, b.updated AS balance_updated, c1.id AS currency_id, c1.name AS currency_name, c1.rate_to_usd AS nearest_rate_to_usd, c1.updated AS rate_date, ROW_NUMBER() OVER ( PARTITION BY b.user_id, b.type, b.currency_id ORDER BY ABS(EXTRACT(EPOCH FROM (b.updated - c1.updated))) ) AS rnk FROM balance b JOIN currency c1 ON b.currency_id = c1.id WHERE c1.updated <= b.updated
codeUNION ALL SELECT b.user_id, b.updated AS balance_updated, c1.id AS currency_id, c1.name AS currency_name, c1.rate_to_usd AS nearest_rate_to_usd, c1.updated AS rate_date, ROW_NUMBER() OVER ( PARTITION BY b.user_id, b.type, b.currency_id ORDER BY ABS(EXTRACT(EPOCH FROM (c1.updated - b.updated))) ) AS rnk FROM balance b JOIN currency c1 ON b.currency_id = c1.id WHERE c1.updated > b.updated), cte_currency_rate AS ( SELECT user_id, currency_id, currency_name, nearest_rate_to_usd, rate_date, ROW_NUMBER() OVER (PARTITION BY user_id, currency_id ORDER BY rnk, rate_date) AS final_rnk FROM cte_nearest_rate ) SELECT COALESCE(u.name, 'not defined') AS name, COALESCE(u.lastname, 'not defined') AS lastname, cr.currency_name, ROUND(b.money * cr.nearest_rate_to_usd, 2) AS currency_in_usd FROM "user" u JOIN balance b ON u.id = b.user_id JOIN ( SELECT user_id, currency_id, currency_name, nearest_rate_to_usd FROM cte_currency_rate WHERE final_rnk = 1 ) cr ON b.user_id = cr.user_id AND b.currency_id = cr.currency_id ORDER BY 1 DESC, 2 ASC, 3 ASC;
- Assistant
- User
решение должно выдавать такой же ответ, как и этот готовый скрипт -- insert into currency values (100, 'EUR', 0.85, '2022-01-01 13:29'); -- insert into currency values (100, 'EUR', 0.79, '2022-01-08 13:29');
CREATE OR REPLACE FUNCTION nearest_rate(p_cur_id bigint, p_date timestamp) RETURNS numeric AS $$ DeCLARE res numeric = NULL; begin SELECT rate_to_usd INTO res FROM ( ( SELECT rate_to_usd, (c.updated - p_date) as time_diff FROM currency C WHERE c.updated > p_date and C.id = p_cur_id ORDER BY time_diff LIMIT 1 ) UNION ( SELECT rate_to_usd, (p_date - c1.updated) as time_diff FROM currency C1 WHERE c1.updated < p_date and C1.id = p_cur_id ORDER BY time_diff LIMIT 1 ) ORDER BY time_diff ASC LIMIT 1 ) R; raise notice 'The number of %', res; return res; end;
- Assistant