-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Task 1 Askergaliyev * Fixed typo in Askergaliyev_1_1.sql * Askergaliyev, task2 is solved. * Askergaliyev 3 task.
- Loading branch information
1 parent
b647810
commit bbdeb93
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
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
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 |
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
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 |