Skip to content

Commit

Permalink
Askergaliyev 3 task. (#129)
Browse files Browse the repository at this point in the history
* Task 1 Askergaliyev

* Fixed typo in Askergaliyev_1_1.sql

* Askergaliyev, task2 is solved.

* Askergaliyev 3 task.
  • Loading branch information
Resolvation authored and mergify[bot] committed Oct 16, 2019
1 parent b647810 commit bbdeb93
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
25 changes: 25 additions & 0 deletions task3/Askergaliyev_3_1.sql
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions task3/Askergaliyev_3_2.sql
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions task3/Askergaliyev_3_3.sql
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit bbdeb93

Please sign in to comment.