zoukankan      html  css  js  c++  java
  • 练习SQL代码

    ---------PS:但凡有聚合函数(where),必然后Group by,Group by后面跟having 
    ---------面试题
    SELECT name,sum(fen) as s
    from score
    GROUP BY NAME
    ORDER BY s;


    PS:一般聚合查询后面跟having

    SELECT title, COUNT(DISTINCT emp_no) AS t FROM titles  --内部再次过滤
    GROUP BY title HAVING t >= 2


    分页查询

    查询第1条到第10条的数据的sql是:select * from table limit 0,10;   ->对应我们的需求就是查询第一页的数据:select * from table limit (1-1)*10,10;

    
    

    查询第10条到第20条的数据的sql是:select * from table limit 10,20;  ->对应我们的需求就是查询第二页的数据:select * from table limit (2-1)*10,10;

    
    

    查询第20条到第30条的数据的sql是:select * from table limit 20,30;  ->对应我们的需求就是查询第三页的数据:select * from table limit (3-1)*10,10;


    --
    ----------------题目--------------------- --1.查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, `first_name` varchar(14) NOT NULL, `last_name` varchar(16) NOT NULL, `gender` char(1) NOT NULL, `hire_date` date NOT NULL, PRIMARY KEY (`emp_no`)); --答案: --子查询 SELECT * FROM employees WHERE hire_date = (SELECT MAX(hire_date) FROM employees); --排序 SELECT * FROM employees ORDER BY hire_date DESC LIMIT 0,1; --2.查找入职员工时间排名倒数第三的员工所有信息 SELECT * FROM employees ORDER BY hire_date DESC LIMIT 2,1; --3.查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_no CREATE TABLE `dept_manager` ( `dept_no` char(4) NOT NULL, `emp_no` int(11) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`dept_no`)); CREATE TABLE `salaries` ( `emp_no` int(11) NOT NULL, `salary` int(11) NOT NULL, `from_date` date NOT NULL, `to_date` date NOT NULL, PRIMARY KEY (`emp_no`,`from_date`)); --Answer: select salaries.emp_no,salaries.salary,salaries.from_date,salaries.to_date,dept_manager.dept_no from salaries inner join dept_manager on dept_manager.emp_no = salaries.emp_no where dept_manager.to_date = '9999-01-01' --没想起来 and salaries.to_date = '9999-01-01'; --4.查找所有已经分配部门的员工的last_name和first_name -- 内链接推荐 SELECT e.last_name,e.first_name,d.dept_no FROM dept_emp d INNER JOIN employees e ON d.emp_no = e.emp_no --left join(左连接):join左表中所有记录和右表中满足连接条件的记录信息 --right join(右连接):join右表中所有记录和左表中满足连接条件的记录信息 --5.查找所有员工的last_name和first_name以及对应部门编号dept_no SELECT e.last_name,e.first_name,d.dept_no FROM employees e LEFT JOIN dept_emp d ON d.emp_no = e.emp_no; --6.查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序 SELECT e.emp_no,s.salary FROM employees e INNER JOIN salaries s ON s.emp_no = e.emp_no AND e.hire_date = s.from_date --少考虑这一种情况 ORDER BY e.emp_no DESC; --7.查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t -- 本题主要考聚合查询 SELECT emp_no, COUNT(emp_no) AS t FROM salaries GROUP BY emp_no HAVING t>15; --8.找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示 SELECT DISTINCT(salary) FROM salaries WHERE to_date='9999-01-01' ORDER BY salary DESC; --9.获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01' -- PS: on已经是条件的判断了,不能再用where, INNER JOIN 左右两表交换 没有 错,oj有问题 SELECT d.dept_no,d.emp_no,s.salary FROM dept_manager d INNER JOIN salaries s ON s.emp_no = d.emp_no AND d.to_date = '9999-01-01' AND s.to_date = '9999-01-01' ---WHERE to_date='9999-01-01'; ---注:有人反映将连接语句改成FROM dept_manager AS d INNER JOIN salaries AS s后,结果通不过。 --INNER JOIN对于左右两表并无顺序要求,此为本题OJ系统Bug所致。 --10.获取所有非manager的员工emp_no !!!!!! -- -PS:考察NOT IN --方法一:使用NOT IN选出在employees但不在dept_manager中的emp_no记录 SELECT emp_no FROM employees WHERE emp_no NOT IN (SELECT emp_no FROM dept_manager) -- 方法二:先使用LEFT JOIN连接两张表,再从此表中选出dept_no值为NULL对应的emp_no记录 SELECT emp_no?FROM?(SELECT * FROM employees LEFT JOIN dept_manager ON employees.emp_no = dept_manager.emp_no) WHERE dept_no IS NULL --11.获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date='9999-01-01'。 --结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_no。 SELECT de.emp_no, dm.emp_no AS manager_no? FROM dept_emp AS de INNER JOIN dept_manager AS dm ON de.dept_no = dm.dept_no? WHERE dm.to_date = '9999-01-01' AND de.emp_no <> dm.emp_no --12.获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary SELECT de.dept_no,de.emp_no,MAX(sa.salary) AS salary FROM dept_emp AS de INNER JOIN salaries AS sa ON de.emp_no = sa.emp_no WHERE de.to_date = '9999-01-01' AND sa.to_date = '9999-01-01' GROUP BY de.dept_no --13.从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。 --PS:正确的过滤是在having以后过滤 SELECT title,COUNT(title) AS t FROM titles WHERE t>=2 --错误 GROUP BY title -- 正确的答案 SELECT title, COUNT(title) AS t FROM titles GROUP BY title HAVING t >= 2; --- 聚合就聚合到这里了 --14.从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。注意对于重复的emp_no进行忽略。 --PS:对COUNT中进行分解 SELECT title, COUNT(DISTINCT emp_no) AS t FROM titles GROUP BY title HAVING t >= 2 --15.查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列 SELECT * FROM employees WHERE emp_no%2 = 1 AND last_name!='Mary' ORDER BY hire_date DESC; --16 .统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg。 --PS:不知道为什么不行 --SELECT t.title AVG(s.salary); --FROM salaries s INNER JOIN titles t --ON s.emp_no = t.emp_no AND t.to_date='9999-01-01' AND s.to_date='9999-01-01' --GROUP BY title select t.title,avg(s.salary) from titles t, salaries s where s.emp_no = t.emp_no and s.to_date =?'9999-01-01' and t.to_date =?'9999-01-01' group by title --17.获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salary SELECT emp_no,salary FROM salaries WHERE to_date='9999-01-01' ORDER BY salary DESC --注意要降序 LIMIT 1,1 --18.查找当前薪水(to_date='9999-01-01')排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by SELECT e.emp_no, MAX(s.salary) AS salary, e.last_name, e.first_name FROM employees AS e INNER JOIN salaries AS s ON e.emp_no = s.emp_no AND s.to_date = '9999-01-01' AND s.salary NOT IN ( SELECT MAX(salary) FROM salaries WHERE salaries.to_date = '9999-01-01'); --19.查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工 --毁三观!!!!!! --1、第一次LEFT --JOIN连接employees表与dept_emp表,得到所有员工的last_name和first_name以及对应的dept_no,也包括暂时没有分配部门的员工 --2、第二次LEFT --JOIN连接上表与departments表,即连接dept_no与dept_name,得到所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工 select e.last_name,e.first_name,d.dept_name from employees e left join dept_emp de on e.emp_no =de.emp_no left join departments d on d.dept_no=de.dept_no; --20.查找员工编号emp_now为10001其自入职以来的薪水salary涨幅值growth SELECT (MAX(salary)-MIN(salary)) AS growth FROM salaries WHERE emp_no = '10001' --16 .统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg。 --PS:不知道为什么不行 --SELECT t.title AVG(s.salary); --FROM salaries s INNER JOIN titles t --ON s.emp_no = t.emp_no AND t.to_date='9999-01-01' AND s.to_date='9999-01-01' --GROUP BY title select t.title,avg(s.salary) from titles t, salaries s where s.emp_no = t.emp_no and s.to_date =?'9999-01-01' and t.to_date =?'9999-01-01' group by title --17.获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salary SELECT emp_no,salary FROM salaries WHERE to_date='9999-01-01' ORDER BY salary DESC --注意要降序 LIMIT 1,1 --18.查找当前薪水(to_date='9999-01-01')排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by SELECT e.emp_no, MAX(s.salary) AS salary, e.last_name, e.first_name FROM employees AS e INNER JOIN salaries AS s ON e.emp_no = s.emp_no AND s.to_date = '9999-01-01' AND s.salary NOT IN ( SELECT MAX(salary) FROM salaries WHERE salaries.to_date = '9999-01-01'); --19.查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工 --毁三观!!!!!! --1、第一次LEFT --JOIN连接employees表与dept_emp表,得到所有员工的last_name和first_name以及对应的dept_no,也包括暂时没有分配部门的员工 --2、第二次LEFT --JOIN连接上表与departments表,即连接dept_no与dept_name,得到所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工 select e.last_name,e.first_name,d.dept_name from employees e left join dept_emp de on e.emp_no =de.emp_no left join departments d on d.dept_no=de.dept_no; --20.查找员工编号emp_now为10001其自入职以来的薪水salary涨幅值growth SELECT (MAX(salary)-MIN(salary)) AS growth FROM salaries WHERE emp_no = '10001' --21.查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_noy以及其对应的薪水涨幅growth,并按照growth进行升序 select t1.emp_no, t1.salary - t2.salary as growth from (select e.emp_no,s.salary from salaries s,employees e where e.emp_no=s.emp_no and s.to_date='9999-01-01' )as t1, (select e.emp_no,s.salary from salaries s,employees e where e.emp_no=s.emp_no and s.from_date=e.hire_date)as t2 where t1.emp_no=t2.emp_no order by growth; --22.统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum -- 多个表查询,可以多次链接 SELECT de.dept_no, dp.dept_name, COUNT(s.salary) AS sum? FROM (dept_emp AS de INNER JOIN salaries AS s ON de.emp_no = s.emp_no)? INNER JOIN departments AS dp ON de.dept_no = dp.dept_no? GROUP BY de.dept_no --23.对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列 SELECT emp_no,salary, AS rank FROM salaries WHERE to_date='9999-01-01' ORDER BY emp_no --24.对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列 select s1.emp_no , s1.salary ,count(distinct s2.salary) as rank from salaries s1, salaries s2 where s1.to_date = '9999-01-01' and s2.to_date = '9999-01-01' and s1.salary<=s2.salary --最大的数只小于等于自己,第二大的数只小于等于两个数,以此类推。。。为他们的rank group by s1.emp_no order by rank ; --25.获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date='9999-01-01' SELECT de.dept_no, s.emp_no, s.salary? FROM (employees AS e INNER JOIN salaries AS s ON s.emp_no = e.emp_no AND s.to_date = '9999-01-01') INNER JOIN dept_emp AS de ON e.emp_no = de.emp_no WHERE de.emp_no NOT IN (SELECT emp_no FROM dept_manager)
    --25.获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01',结果第一列给出员工的emp_no,第二列给出其manager的manager_no,第三列给出该员工当前的薪水emp_salary,第四列给该员工对应的manager当前的薪水manager_salary
    --SELECT 
    --FROM dept_emp e INNER JOIN dept_manager m
    --ON e.emp_no = m.emp_no
    
    --本题主要思想是创建两张表(一张记录当前所有员工的工资,另一张只记录部门经理的工资)进行比较,具体思路如下: 
    -- 1、先用INNER JOIN连接salaries和demp_emp,建立当前所有员工的工资记录sem 
    -- 2、再用INNER JOIN连接salaries和demp_manager,建立当前所有员工的工资记录sdm 
    -- 3、最后用限制条件sem.dept_no = sdm.dept_no AND sem.salary >
    -- sdm.salary找出同一部门中工资比经理高的员工,并根据题意依次输出emp_no、manager_no、emp_salary、manager_salary
    
    SELECT sem.emp_no AS emp_no, sdm.emp_no AS manager_no, sem.salary AS emp_salary, sdm.salary AS manager_salary
    FROM (SELECT s.salary, s.emp_no, de.dept_no FROM salaries s INNER JOIN dept_emp de 
    ON s.emp_no = de.emp_no AND s.to_date = '9999-01-01' ) AS sem, 
    (SELECT s.salary, s.emp_no, dm.dept_no FROM salaries s INNER JOIN dept_manager dm 
    ON s.emp_no = dm.emp_no AND s.to_date = '9999-01-01' ) AS sdm
    WHERE sem.dept_no = sdm.dept_no AND sem.salary > sdm.salary
    
    --26.汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
    --PS:对一个表进行连接以后,可以再连接两一个表
    
    本题的关键在于用 GROUP BY 同时对 de.dept_no 和 t.title
    进行分组,具体思路如下: 
    1、先用 INNER JOIN 连接 dept_emp 与 salaries,根据测试数据添加限定条件 de.to_date =
    '9999-01-01' AND t.to_date = '9999-01-01',即当前员工的当前头衔 
    2、再用 INNER JOIN 连接departments,限定条件为 de.dept_no =
    dp.dept_no,即部门编号相同 
    3、最后用 GROUP BY 同时对 de.dept_no 和 t.title   进行分组,用 COUNT(t.title)
    统计相同部门下相同头衔的员工个数 
    
    SELECT de.dept_no, dp.dept_name, t.title, COUNT(t.title) AS count
    FROM titles AS t INNER JOIN dept_emp AS de 
    ON t.emp_no = de.emp_no AND de.to_date = '9999-01-01' AND t.to_date = '9999-01-01'
    INNER JOIN departments AS dp 
    ON de.dept_no = dp.dept_no
    GROUP BY de.dept_no, t.title
    
    --27.给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。
    -- 提示:在sqlite中获取datetime时间对应的年份函数为strftime('%Y', to_date)
    
    --SELECT emp_no,from_date,  AS salary_growth
    --FROM salaries
    
    SELECT s2.emp_no, s2.from_date, (s2.salary - s1.salary) AS salary_growth
    FROM salaries AS s1, salaries AS s2
    WHERE s1.emp_no = s2.emp_no 
    AND salary_growth > 5000
    AND (strftime("%Y",s2.to_date) - strftime("%Y",s1.to_date) = 1 
         OR strftime("%Y",s2.from_date) - strftime("%Y",s1.from_date) = 1 )
    ORDER BY salary_growth DESC
    
    --28.查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
    
    SELECT c.name AS name, COUNT(c.name) AS amount
    FROM (film AS f INNER JOIN film_category AS fc ON f.film_id = fc.film_id )
    INNER JOIN category AS c
    ON fc.category_id = c.category_id
    WHERE f.description LIKE '%robot%'
    GROUP BY c.name HAVING amount >= 2
    
    --29.使用join查询方式找出没有分类的电影id以及名称
    
    SELECT f.film_id, f.title FROM film f LEFT JOIN film_category fc
    ON f.film_id = fc.film_id WHERE fc.category_id IS NULL
    
    --30.使用子查询的方式找出属于Action分类的所有电影对应的title,description
    
    SELECT f.title, f.description 
    FROM film f, film_category fc, category c
    WHERE f.film_id = fc.film_id 
    AND fc.category_id = c.category_id 
    AND c.name = 'Action'
    
    --32.将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
    
    --不同数据库连接字符串的方法不完全相同,MySQL、SQL
    --Server、Oracle等数据库支持CONCAT方法,而本题所用的SQLite数据库只支持用连接符号"||"来连接字符串 
    
    SELECT last_name||" "||first_name AS Name FROM employees
    
    --33.创建一个actor表,包含如下列信息
    
    CREATE TABLE IF NOT EXISTS actor 
    ( 
        actor_id smallint(5) NOT NULL PRIMARY KEY,
        first_name varchar(45) NOT NULL, 
        last_name varchar(45) NOT NULL, 
       last_update timestamp NOT NULL DEFAULT (datetime('now','localtime'))
    ) 
    
    --34.批量插入数据
    
    INSERT INTO actor 
    VALUES (1, 'PENELOPE', 'GUINESS', '2006-02-15 12:34:33'),
    (2, 'NICK', 'WAHLBERG', '2006-02-15 12:34:33')
    
    --35.批量插入数据
    INSERT OR IGNORE INTO actor VALUES (3, 'ED', 'CHASE', '2006-02-15 12:34:33')
    
    --36.创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。 actor_name表结构如下:
    
    CREATE TABLE actor_name
    (
        first_name varchar(45) NOT NULL,
        last_name varchar(45) NOT NULL
    );
    INSERT INTO actor_name SELECT first_name, last_name FROM actor;
    
    --37.对first_name创建唯一索引uniq_idx_firstname,对last_name创建普通索引idx_lastname
    
    CREATE UNIQUE INDEX uniq_idx_firstname ON actor(first_name);
    CREATE INDEX idx_lastname ON actor(last_name);
    
    --38.针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,并对这两列重新命名,fist_name为first_name_v,last_name修改为last_name_v:
    CREATE TABLE IF NOT EXISTS actor (
    actor_id smallint(5) NOT NULL PRIMARY KEY,
    first_name varchar(45) NOT NULL,
    last_name varchar(45) NOT NULL,
    last_update timestamp NOT NULL DEFAULT (datetime('now','localtime')))
    -------------------------------
    --视图是用来干什么的?视图时虚表,仅存储建立视图的sql语句,查询的时候跟普通的表一样,
    CREATE VIEW actor_name_view AS
    SELECT first_name AS fist_name_v, last_name AS last_name_v
    FROM actor 
    
    --39.针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使用强制索引。
    SELECT * FROM salaries INDEXED BY idx_emp_no WHERE emp_no='10005';
    
    --40.现在在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为'0000 00:00:00'
    --ADD COLUMN create_date 
    ALTER TABLE actor ADD COLUMN create_date datetime NOT NULL DEFAULT '0000-00-00 00:00:00';
    
    --41.构造一个触发器audit_log,在向employees表中插入一条数据的时候,触发插入相关的数据到audit中。
    CREATE TRIGGER audit_log AFTER INSERT ON employees_test
    BEGIN
        INSERT INTO audit VALUES (NEW.ID, NEW.NAME);
    END;
    
    --42.删除emp_no重复的记录,只保留最小的id对应的记录。
    DELETE FROM titles_test WHERE id NOT IN 
    (SELECT MIN(id) FROM titles_test GROUP BY emp_no);
    
    --43.将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。
    UPDATE FROM titles_test SET to_date =NULL, from_date='2001-01-01'
    WHERE to_date='9999-01-01';
    
    --44.将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005,其他数据保持不变,使用replace实现。
    update titles_test set emp_no=replace(emp_no,'10001','10005') where id='5'
    
    --45.将titles_test表名修改为titles_2017。
    -- RENAME TABLE titles_test AS titles_2017;
    ALTER TABLE titles_test RENAME TO titles_2017;
    
    --46.在audit表上创建外键约束,其emp_no对应employees_test表的主键id。
    --CREATE FOREIGN KEY for   REFERENCES ON  
    
    --47.存在如下的视图:create view emp_v as select * from employees where emp_no >10005;如何获取emp_v和employees有相同的数据?
    --PS :使用INTERSECT取交集
    SELECT * FROM employees INTERSECT SELECT * FROM emp_v
    
    --48.将所有获取奖金的员工当前的薪水增加10%。
    update salaries
    set salary=1.1*salary
    where emp_no in (select emp_no from emp_bonus)
    
    --50.将employees表中的所有员工的last_name和first_name通过(')连接起来。
    SELECT last_name ||  "'"  || first_name  AS name FROM employees;
    
    --51.查找字符串'10,A,B' 中逗号','出现的次数cnt
    SELECT (length("10,A,B")-length(replace("10,A,B",",","")))/length(",") AS cnt
    
    --52.获取Employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列
    SELECT first_name
    FROM employees
    ORDER BY substr(first_name,-2) ;
    
    --53.按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employees
    --SELECT dept_no,(SELECT ) AS employees
    --FROM dept_emp
    
    --本题要用到SQLite的聚合函数group_concat(X,Y),其中X是要连接的字段,Y是连接时用的符号,可省略,默认为逗号。此函数必须与 GROUP BY 配合使用。此题以 dept_no 作为分组,将每个分组中不同的emp_no用逗号连接起来(即可省略Y)。可参考:       http://www.sqlite.org/lang_aggfunc.html#groupconcat         http://blog.csdn.net/langzxz/article/details/16807859    1
    
    SELECT dept_no, group_concat(emp_no) AS employees
    FROM dept_emp GROUP BY dept_no
    
    --54.查找排除当前最大、最小salary之后的员工的平均工资avg_salary。
    --PS:不会选择最大和最小
    --SELECT AVG(salary) AS avg_salary
    --FROM salaries
    
    SELECT AVG(salary) AS avg_salary FROM salaries 
    WHERE to_date = '9999-01-01' 
    AND salary NOT IN (SELECT MAX(salary) FROM salaries WHERE to_date = '9999-01-01')
    AND salary NOT IN (SELECT MIN(salary) FROM salaries WHERE to_date = '9999-01-01')
    
    --55.分页查询employees表,每5行一页,返回第2页的数据
    SELECT * FROM employees LIMIT 5,5
    
    --56.获取所有员工的emp_no、部门编号dept_no以及对应的bonus类型btype和recevied,没有分配具体的员工不显示
    
    SELECT em.emp_no, de.dept_no, eb.btype, eb.recevied
    FROM employees AS em INNER JOIN dept_emp AS de
    ON em.emp_no = de.emp_no
    LEFT JOIN emp_bonus AS eb 
    ON de.emp_no = eb.emp_no
    
    --57.使用含有关键字exists查找未分配具体部门的员工的所有信息。
    
    SELECT * FROM employees WHERE NOT EXISTS 
    (SELECT emp_no FROM dept_emp WHERE emp_no = employees.emp_no)
    
    SELECT * FROM employees WHERE emp_no NOT IN (SELECT emp_no FROM dept_emp)
    
    --58.获取employees中的行数据,且这些行也存在于emp_v中。注意不能使用intersect关键字。
    --根据题意,不能使用 INTERSECT 关键字,但由于视图 emp_v 的记录是从 employees 中导出的,因此要判断两者中相等的数据,只需要判断emp_no相等即可。 
    --   方法一:用 WHERE 选取二者 emp_no 相等的记录  1
    SELECT em.* FROM employees AS em, emp_v AS ev WHERE em.emp_no = ev.emp_no
    
    --59.获取有奖金的员工相关信息.
    --给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。 
    --bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。 当前薪水表示to_date='9999-01-01'
    SELECT e.emp_no, e.first_name, e.last_name, b.btype, s.salary, 
    (CASE b.btype 
     WHEN 1 THEN s.salary * 0.1
     WHEN 2 THEN s.salary * 0.2
     ELSE s.salary * 0.3 END) AS bonus
    FROM employees AS e INNER JOIN emp_bonus AS b ON e.emp_no = b.emp_no
    INNER JOIN salaries AS s ON e.emp_no = s.emp_no AND s.to_date = '9999-01-01'
    
    --60.按照salary的累计和running_total,其中running_total为前两个员工的salary累计和,其他以此类推。 具体结果如下Demo展示。
    --SELECT emp_no,salary, ADD(salary) AS running_total
    --FROM salaries
    
    SELECT s1.emp_no, s1.salary, 
    (SELECT SUM(s2.salary) FROM salaries AS s2 
     WHERE s2.emp_no <= s1.emp_no AND s2.to_date = '9999-01-01') AS running_total 
    FROM salaries AS s1 WHERE s1.to_date = '9999-01-01'
    
    --61.对于employees表中,给出奇数行的first_name
    --SELECT first_name
    --FROM employees
    --WHERE 
    
    SELECT e.first_name FROM employees AS e
    WHERE e.first_name IN(
        SELECT e1.first_name
        FROM employees AS e1 INNER JOIN employees AS e2
        ON e1.first_name >= e2.first_name
        GROUP BY e1.first_name HAVING count(e2.first_name) % 2 = 1);

     乔虹姐帮忙修改

    SELECT * FROM PTE_ProfessionalTitleApplication
    
    
    SELECT t.* FROM EEP_Base.dbo.Users t
    
    SELECT t.* FROM EEP_Base.dbo.db_tmp t
    
    SELECT * from EEP_Base.dbo.Department
    
    SELECT t.userType FROM EEP_Base.dbo.db_tmp t
    
    SELECT t.*,u.ParentId FROM EEP_Base.dbo.db_tmp t
    inner join EEP_Base.dbo.Users u
    on u.WorkerNO=t.workId
    
    
    SELECT  FROM EEP_Base.dbo.db_tmp t
    inner join EEP_Base.dbo.Users u
    on u.WorkerNO=t.workId
    
    
    SELECT u.ParentID FROM  EEP_Base.dbo.Users u
    inner join EEP_Base.dbo.Department d
    on u.ParentID=d.ID
    inner join EEP_Base.dbo.db_tmp t
    on u.WorkerNO=t.workId
    
    
    SELECT t.* FROM EEP_Base.dbo.db_tmp t 
    WHERE t.workId in (SELECT u.*,u.Number FROM  EEP_Base.dbo.Users u
    inner join EEP_Base.dbo.Department d
    on u.ParentID=d.ID)
    
    
    SELECT u.name,u.Number FROM EEP_Base.dbo.Users u
    WHERE u.WorkerNO in (SELECT t.workId FROM EEP_Base.dbo.db_tmp t)
     
    
    insert into db_tmp(number) SELECT u.Number FROM EEP_Base.dbo.db_tmp t
    inner join EEP_Base.dbo.Users u
    on u.WorkerNO=t.workId
    
    
    UPDATE db_tmp  SET(number) =(SELECT u.Number FROM  EEP_Base.dbo.Users u
    WHERE u.WorkerNO=db_tmp.workId)
    
    
    UPDATE EEP_Base.dbo.db_tmp  SET deptName =d.name FROM  EEP_Base.dbo.Users u, EEP_Base.dbo.db_tmp t,EEP_Base.dbo.Department d
    WHERE u.WorkerNO=t.workId
    AND u.ParentID=d.ID

     帮李姐整理的数据(没有用户ID)

    需求:

    只有红色的两列,找出人员编号和部门编号;
    步骤:
    1. 查找不重复的数据,把人员编号补充进去
    SELECT t.ID,u.Number,u.name, t.Username FROM EEP_Base.dbo.tb_temp t
    LEFT join EEP_Base.dbo.Users u
    on u.Name=t.Username 
    WHERE u.name !='刘x'
    and u.name !='刘x'
    and u.name !='张建x'
    
    ORDER BY t.ID

    2、查找重复人名 的部门的人,补充部门

    SELECT u.Name,d.Name,u.Number FROM EEP_Base.dbo.Users u
    inner join EEP_Base.dbo.Department d
    on u.ParentID=d.ID
    WHERE u.Name='沈中华'

    3、找到部门编号填充

    SELECT Name,Description FROM EEP_Base.dbo.Department

    4、校验数据的正确性

    SELECT t.ID,u.Name,d.Name,t.DeptName,u.Number,t.UserNumber FROM EEP_Base.dbo.Users u
    inner join EEP_Base.dbo.Department d
    on u.ParentID=d.ID
    inner join EEP_Base.dbo.tb_temp t
    ON u.Number=t.UserNumber
    --WHERE u.Number!=t.UserNumber
    ORDER BY t.ID

     李姐布置的sql查询

    2、各部门本月里程碑节点完成率、年度目标完成率(未完成)
    SELECT aa.OwnerDepartmentID,d.name,aa.sum1 AS '里程碑个数',aa.sum2 AS '已完成里程碑个数',ltrim(Convert(numeric(9,2),sum2*100.0/sum1))+'%' As '百分比' FROM
     
    (SELECT OwnerDepartmentID,SUM(t1.cc) AS sum1,ISNULL(SUM(t2.cc), 0) as sum2  FROM ProjectArchive.dbo.Project AS p
    INNER JOIN (SELECT COUNT(t.ID) as cc ,t.ProjectID as ProjectID FROM ProjectArchive.dbo.ISOForm io
    INNER JOIN ProjectArchive.dbo.PlanFormProjectTask t
    ON t.ProjectID=io.ProjectID  and io.RowGuid=t.FormRowGuid
    WHERE io.FlowState>=4 and io.IsAvailability=1
    AND t.IsEventTask=1
    AND t.TaskState<>3
    AND t.EndDate>='2020-10-01 00:00:00'
    AND t.EndDate<'2020-11-01 00:00:00'
    GROUP BY t.ProjectID) as t1 
    ON t1.ProjectID=p.ID
    left JOIN (SELECT COUNT(t.ID) as cc ,t.ProjectID as ProjectID FROM ProjectArchive.dbo.ISOForm io
    INNER JOIN ProjectArchive.dbo.PlanFormProjectTask t
    ON t.ProjectID=io.ProjectID  and io.RowGuid=t.FormRowGuid
    WHERE io.FlowState>=4 and io.IsAvailability=1
    AND t.IsEventTask=1
    AND t.TaskState=2
    AND t.EndDate>='2020-10-01 00:00:00'
    AND t.EndDate<='2020-11-01 00:00:00'
    GROUP BY t.ProjectID) as t2 
    ON t2.ProjectID=p.ID
    GROUP BY p.OwnerDepartmentID) AS aa
    
    INNER JOIN EEP_Base.dbo.Department d
    ON aa.OwnerDepartmentID=d.Id
    3、计划变更项目清单
    
    SELECT p.ID,p.Name,sp.Number,sp.Name,aa.date1 as '第一次时间',bb.date2 as '最新时间'
    FROM ProjectArchive.dbo.Project p
    INNER JOIN ProjectArchive.dbo.SubProject sp 
    ON p.ID=sp.ProjectID
     left join(SELECT FormContent.query('PlanFormContent/PlanReleaseDateStr' ).value('(//PlanReleaseDateStr)[1]', 'varchar(50)') as date1,subprojectid FROM ProjectArchive.dbo.ISOForm io
    WHERE io.FlowState>=4 and io.VersionNumber=1) as aa on sp.id=aa.SubProjectid 
    left join(SELECT FormContent.query('PlanFormContent/PlanReleaseDateStr' ).value('(//PlanReleaseDateStr)[1]', 'varchar(50)') as date2,subprojectid FROM ProjectArchive.dbo.ISOForm io
    WHERE io.FlowState>=4 and io.IsAvailability=1) as bb on sp.id=bb.SubProjectid 
    
    
    4、未及时发布计划项目清单
    
    SELECT p.ID,p.Name FROM ProjectArchive.dbo.Project p
    LEFT  JOIN  ProjectArchive.dbo.ISOForm io
    ON io.ProjectId=p.Id
    
    WHERE io.FlowState>=4 and io.VersionNumber=1
    AND DATEDIFF(day,ValidDate,FormContent.query('PlanFormContent/PlanReleaseDateStr' ).value('(//PlanReleaseDateStr)[1]', 'varchar(50)'))>20 
    
    UNION
    SELECT p.id,p.Name
    from ProjectArchive.dbo.Project as p
    where p.ProjectState=20 and DATEDIFF(day,ValidDate,GETDATE())>20  and (SELECT Count(id) from ProjectArchive.dbo.ISOForm where Projectid=p.id)=0










  • 相关阅读:
    css盒模型不同浏览器下解释不同 解决办法
    【META http-equiv="Content-Type" Content="text/html; Charset=*】意义详解
    淘宝2015年秋招在线笔试题
    mouseleave mouseout时候悬浮框不应该消失的时候消失了 css 解决办法
    ACM知识点分类
    2019牛客多校第九场 B.Quadratic equation
    扫描线算法
    可持久化数据结构(模板)
    luogu SP3267 DQUERY
    luogu2633 Count on a tree(树上LCA+主席树求区间第k小)
  • 原文地址:https://www.cnblogs.com/bee-home/p/7436561.html
Copyright © 2011-2022 走看看