From bbdeb93d393f47c7764667eb4a5df8058efdd581 Mon Sep 17 00:00:00 2001 From: Resolvation <32676455+Resolvation@users.noreply.github.com> Date: Wed, 16 Oct 2019 23:55:44 +0600 Subject: [PATCH] Askergaliyev 3 task. (#129) * Task 1 Askergaliyev * Fixed typo in Askergaliyev_1_1.sql * Askergaliyev, task2 is solved. * Askergaliyev 3 task. --- task3/Askergaliyev_3_1.sql | 25 +++++++++++++++++++++++++ task3/Askergaliyev_3_2.sql | 25 +++++++++++++++++++++++++ task3/Askergaliyev_3_3.sql | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 task3/Askergaliyev_3_1.sql create mode 100644 task3/Askergaliyev_3_2.sql create mode 100644 task3/Askergaliyev_3_3.sql diff --git a/task3/Askergaliyev_3_1.sql b/task3/Askergaliyev_3_1.sql new file mode 100644 index 00000000..bd93dba1 --- /dev/null +++ b/task3/Askergaliyev_3_1.sql @@ -0,0 +1,25 @@ +SELECT + customers.customer_rk, + customers.full_name, + full_name_popularity.popularity +FROM ( + SELECT + customer_rk, + CONCAT(last_nm, ':', first_nm, ':', middle_nm) AS full_name + FROM + cd_customers + WHERE + valid_to_dttm = '5999-01-01 00:00:00' + ) AS customers +LEFT JOIN ( + SELECT + CONCAT(last_nm, ':', first_nm, ':', middle_nm) AS full_name, + COUNT(*) AS popularity + FROM + cd_customers + WHERE + valid_to_dttm = '5999-01-01 00:00:00' + GROUP BY + full_name + ) AS full_name_popularity +ON customers.full_name = full_name_popularity.full_name diff --git a/task3/Askergaliyev_3_2.sql b/task3/Askergaliyev_3_2.sql new file mode 100644 index 00000000..5b5bef66 --- /dev/null +++ b/task3/Askergaliyev_3_2.sql @@ -0,0 +1,25 @@ +SELECT + calendar_dt, + IFNULL(SUM(open_accounts), 0) AS num_open_accounts +FROM + calendar +LEFT JOIN ( + SELECT + renewed_dt, + expiration_dt, + COUNT(*) AS open_accounts + FROM + account_periods + GROUP BY + renewed_dt, expiration_dt + ) AS open_close_dts +ON renewed_dt <= calendar_dt AND calendar_dt < expiration_dt +WHERE + calendar_dt BETWEEN ( + SELECT + MIN(renewed_dt) + FROM + account_periods + ) AND CURDATE() +GROUP BY + calendar_dt diff --git a/task3/Askergaliyev_3_3.sql b/task3/Askergaliyev_3_3.sql new file mode 100644 index 00000000..9b52662f --- /dev/null +++ b/task3/Askergaliyev_3_3.sql @@ -0,0 +1,38 @@ +SELECT + years.year_no, + IFNULL(left_table.n_opens, 0) AS n_opens, + IFNULL(left_table.n_prolongations, 0) AS n_prolongations, + IFNULL(right_table.n_increased_deposits, 0) AS n_increased_deposits +FROM ( + SELECT DISTINCT + year_no + FROM + calendar + ) AS years +LEFT JOIN ( + SELECT + YEAR(renewed_dt) AS action_year, + SUM(account_renewal_cnt = 1) AS n_opens, + SUM(account_renewal_cnt > 1) AS n_prolongations + FROM + account_periods + GROUP BY + action_year + ) AS left_table +ON years.year_no = left_table.action_year +LEFT JOIN ( + SELECT + YEAR(former_table.renewed_dt) AS action_year, + COUNT(*) AS n_increased_deposits + FROM + account_periods AS former_table + INNER JOIN + account_periods AS latter_table + ON + former_table.account_rk = latter_table.account_rk + AND 2 * former_table.opening_amt > 3 * latter_table.opening_amt + AND former_table.account_renewal_cnt = latter_table.account_renewal_cnt + 1 + GROUP BY + action_year + ) AS right_table +ON years.year_no = right_table.action_year