#数据库查询

ALTER TABLE emp ADD updated_salary DECIMAL(10,4); 
UPDATE emp
SET updated_salary =
    CASE
        WHEN job = 'CLERK' THEN sal * 1.1
        WHEN job = 'SALESMAN' THEN sal * 1.15
        WHEN job = 'PRESIDENT' THEN sal * 0.9
        WHEN job = 'MANAGER' THEN sal * 1.05
        WHEN job = 'ANALYST' THEN sal * 1.2
        ELSE sal 
    END;

ALTER TABLE emp ADD newsalgrade NUMBER; 
UPDATE emp
SET newsalgrade = (
  SELECT hisal
  FROM salgrade
  WHERE emp.updated_salary BETWEEN losal AND hisal
)
WHERE updated_salary IS NOT NULL;

SELECT newsalgrade, COUNT(*) AS num_of_employees
FROM emp
GROUP BY newsalgrade;

ALTER TABLE emp ADD oldsalgrade NUMBER; 
UPDATE emp
SET oldsalgrade = (
  SELECT hisal
  FROM salgrade
  WHERE emp.sal BETWEEN losal AND hisal
)
WHERE sal IS NOT NULL;

SELECT oldsalgrade, COUNT(*) AS num_of_employees
FROM emp
GROUP BY oldsalgrade;

SELECT sg.hisal AS "salary_grade",
       COUNT(CASE WHEN e.oldsalgrade = sg.hisal THEN 1 END) AS "num_of_employees_before",
       COUNT(CASE WHEN e.newsalgrade = sg.hisal THEN 1 END) AS "num_of_employees_after",
       COUNT(CASE WHEN e.oldsalgrade = sg.hisal THEN 1 END) - COUNT(CASE WHEN e.newsalgrade = sg.hisal THEN 1 END) AS "num_of_employee_changes"
FROM salgrade sg
LEFT JOIN emp e ON sg.hisal = e.oldsalgrade OR sg.hisal = e.newsalgrade
GROUP BY sg.hisal;

SELECT 
    d.DEPTNO,
    d.DNAME,
    COUNT(CASE WHEN e.OLDSALGRADE <> e.NEWSALGRADE THEN e.EMPNO END) AS "薪水等级发生变化客户数"
FROM 
    emp e INNER JOIN dept d ON e.DEPTNO = d.DEPTNO
GROUP BY 
    d.DEPTNO, d.DNAME;
 

SELECT 
    d.DEPTNO AS '部门编号',
    d.DNAME AS '部门名称',
    SUM(CASE WHEN e.NEWSALGRADE = 1200 THEN 1 ELSE 0 END) AS "薪水等级1级职员数",
    SUM(CASE WHEN e.NEWSALGRADE = 1400 THEN 1 ELSE 0 END) AS "薪水等级2级职员数",
    SUM(CASE WHEN e.NEWSALGRADE = 2000 THEN 1 ELSE 0 END) AS "薪水等级3级职员数",
    SUM(CASE WHEN e.NEWSALGRADE = 3000 THEN 1 ELSE 0 END) AS "薪水等级4级职员数",
    SUM(CASE WHEN e.NEWSALGRADE = 9999 THEN 1 ELSE 0 END) AS "薪水等级5级职员数",
    COUNT(CASE WHEN e.OLDSALGRADE <> e.NEWSALGRADE THEN e.EMPNO END) AS "薪水等级发生变化客户数"
FROM 
    emp e INNER JOIN dept d ON e.DEPTNO = d.DEPTNO
GROUP BY 
    d.DEPTNO, d.DNAME
ORDER BY
    d.DEPTNO;
 
SELECT 
    d.DEPTNO AS '部门编号',
    COALESCE(d.DNAME, '未知') AS '部门名称',
    SUM(CASE WHEN e.NEWSALGRADE = 1 THEN 1 ELSE 0 END) AS '薪水等级1级职员数',
    SUM(CASE WHEN e.NEWSALGRADE = 2 THEN 1 ELSE 0 END) AS '薪水等级2级职员数',
    SUM(CASE WHEN e.NEWSALGRADE = 3 THEN 1 ELSE 0 END) AS '薪水等级3级职员数',
    SUM(CASE WHEN e.NEWSALGRADE = 4 THEN 1 ELSE 0 END) AS '薪水等级4级职员数',
    SUM(CASE WHEN e.NEWSALGRADE = 5 THEN 1 ELSE 0 END) AS '薪水等级5级职员数',
    COUNT(CASE WHEN e.OLDSALGRADE <> e.NEWSALGRADE THEN e.EMPNO END) AS '薪水等级发生变化客户数'
FROM 
    emp e LEFT JOIN dept d ON e.DEPTNO = d.DEPTNO
GROUP BY 
    d.DEPTNO, d.DNAME
ORDER BY
    d.DEPTNO;
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值