0% found this document useful (0 votes)
20 views8 pages

hw6

Uploaded by

nerijaross542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

hw6

Uploaded by

nerijaross542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

B
2. D
3. D
4. D
5. A
6. A

1.

with step1(branch_name,emp_count) as(


select b.NAME as branch_name,count(*) as emp_count
from branch b
left join employee e
on b.BRANCH_ID = e.ASSIGNED_BRANCH_ID
group by b.BRANCH_ID
),
step2(branch_name,cust_count) as(
select b.NAME as branch_name,count(*) as cust_count
from branch b
left join employee e
on b.BRANCH_ID = e.ASSIGNED_BRANCH_ID
left join account a
on e.EMP_ID = a.OPEN_EMP_ID
left join customer c
on a.CUST_ID = c.CUST_ID
group by b.BRANCH_ID
)
select *
from step1
natural join step2;
2.

with step1(branch_id,superior_emp_id,emp_count) as(


select b.BRANCH_ID as branch_id, e.SUPERIOR_EMP_ID as superior_emp_id, count(*) as
emp_count
from employee e
left join branch b
on e.ASSIGNED_BRANCH_ID = b.BRANCH_ID
group by b.BRANCH_ID, e.SUPERIOR_EMP_ID
order by count(*) desc
),
step2(branch_id,max_emp_count) as(
select branch_id, max(emp_count)as max_emp_count
from step1
group by branch_id
order by branch_id desc
),
step3(emp_id,emp_count) as(
select step1.superior_emp_id as emp_id, step1.emp_count as emp_count
from step1
join step2
on step1.branch_id = step2.branch_id
where step1.emp_count = step2.max_emp_count
)
select e.EMP_ID, concat(e.LAST_NAME,e.FIRST_NAME) as 全名, b.NAME,step3.emp_count
from step3
left join employee e
on step3.emp_id = e.EMP_ID
left join branch b
on e.ASSIGNED_BRANCH_ID = b.BRANCH_ID
where step3.emp_id = e.emp_id;
3.
with step1(dept_id, min_avail_balance)as(
select e.DEPT_ID as dept_id, min(a.AVAIL_BALANCE) as min_avail_balance
from employee e
left join account a
on e.EMP_ID = a.OPEN_EMP_ID
group by e.DEPT_ID
)
select distinct d.NAME as 部门名, p.PRODUCT_TYPE_CD as 账号产品类型, a.AVAIL_BALANCE as 余额
from step1
left join department d
on d.DEPT_ID = step1.dept_id
left join employee e
on e.DEPT_ID = step1.dept_id
left join account a
on e.EMP_ID = a.OPEN_EMP_ID
left join product p
on a.PRODUCT_CD = p.PRODUCT_CD
where step1.min_avail_balance = a.AVAIL_BALANCE;
4.
with step1(max_amount,account_id) as(
select max(acc.AMOUNT) as max_amount, a.ACCOUNT_ID as account_id
from account a
left join acc_transaction acc
on a.ACCOUNT_ID = acc.ACCOUNT_ID
left join customer c
on a.CUST_ID = c.CUST_ID
group by a.ACCOUNT_ID
)
select
CASE
WHEN c.CUST_TYPE_CD = 'I' THEN concat(i.LAST_NAME, i.FIRST_NAME)
ELSE b.NAME
END as 客户全名,
a.ACCOUNT_ID as 账户 ID,
acc.TXN_ID as 交易 ID,
acc.AMOUNT as 交易金额,
acc.TXN_DATE as 交易日期
from step1
left join account a
on step1.account_id = a.ACCOUNT_ID
left join acc_transaction acc
on a.ACCOUNT_ID = acc.ACCOUNT_ID
left join customer c
on a.CUST_ID = c.CUST_ID
left join individual i
on c.CUST_ID = i.CUST_ID
left join business b
on c.CUST_ID = b.CUST_ID
where step1.max_amount = acc.AMOUNT;
5.
WITH RECURSIVE CoursePrereq AS (
SELECT
CAST(course_id AS CHAR(50)) COLLATE utf8mb4_general_ci AS course_id,
CAST(prereq_id AS CHAR(50)) COLLATE utf8mb4_general_ci AS prereq_id
FROM prereq

UNION ALL

SELECT
CAST(course_id AS CHAR(50)) COLLATE utf8mb4_general_ci AS course_id,
CAST(course_id AS CHAR(50)) COLLATE utf8mb4_general_ci AS prereq_id
FROM prereq

UNION ALL
SELECT
CAST(p.course_id AS CHAR(50)) COLLATE utf8mb4_general_ci AS course_id,
CAST(c.prereq_id AS CHAR(50)) COLLATE utf8mb4_general_ci AS prereq_id
FROM prereq p
JOIN CoursePrereq c
ON p.prereq_id COLLATE utf8mb4_general_ci = c.course_id COLLATE utf8mb4_general_ci
)

SELECT DISTINCT
course_id,
prereq_id
FROM CoursePrereq
ORDER BY course_id, prereq_id;
6.

DELIMITER $$

CREATE FUNCTION step1(course_id VARCHAR(8))


RETURNS TEXT
DETERMINISTIC
BEGIN
DECLARE prereq_list TEXT DEFAULT course_id;
DECLARE new_prereqs TEXT DEFAULT course_id;
DECLARE current_prereqs TEXT;
DECLARE done INT DEFAULT 0;

WHILE done = 0 DO
SET current_prereqs = new_prereqs;

SELECT GROUP_CONCAT(DISTINCT p.prereq_id) INTO new_prereqs


FROM prereq p
WHERE FIND_IN_SET(p.course_id, current_prereqs) > 0
AND NOT FIND_IN_SET(p.prereq_id, prereq_list);

IF new_prereqs IS NULL THEN


SET done = 1;
ELSE
SET prereq_list = CONCAT(prereq_list, ',', new_prereqs);
END IF;
END WHILE;

RETURN prereq_list;
END$$

DELIMITER ;

You might also like