题解 | #查找在职员工自入职以来的薪水涨幅情况#
查找在职员工自入职以来的薪水涨幅情况
http://www.nowcoder.com/practice/fc7344ece7294b9e98401826b94c6ea5
用单表自连接得到:
SELECT S1.emp_no, MAX ( S1.salary ) - MIN ( S2.salary ) AS growth FROM salaries AS S1 INNER JOIN salaries AS s2 ON S1.emp_no = S2.emp_no WHERE S1.to_date = '9999-01-01' GROUP BY S1.emp_no ORDER BY growth;
当去掉所有条件限制时,得到的数据如下:(inner join和cross join 的结果相同,请问是不是在自联结中,二者的效果就是一样的?)
10001|85097|2001-06-22|2002-06-22|10001|85097|2001-06-22|2002-06-22
10001|85097|2001-06-22|2002-06-22|10001|88958|2002-06-22|9999-01-01
10001|88958|2002-06-22|9999-01-01|10001|85097|2001-06-22|2002-06-22
10001|88958|2002-06-22|9999-01-01|10001|88958|2002-06-22|9999-01-01
10002|72527|1999-08-03|2000-08-02|10002|72527|1999-08-03|2000-08-02
10002|72527|1999-08-03|2000-08-02|10002|72527|2000-08-02|2001-08-02
10002|72527|2000-08-02|2001-08-02|10002|72527|1999-08-03|2000-08-02
10002|72527|2000-08-02|2001-08-02|10002|72527|2000-08-02|2001-08-02
10001|85097|2001-06-22|2002-06-22|10001|88958|2002-06-22|9999-01-01
10001|88958|2002-06-22|9999-01-01|10001|85097|2001-06-22|2002-06-22
10001|88958|2002-06-22|9999-01-01|10001|88958|2002-06-22|9999-01-01
10002|72527|1999-08-03|2000-08-02|10002|72527|1999-08-03|2000-08-02
10002|72527|1999-08-03|2000-08-02|10002|72527|2000-08-02|2001-08-02
10002|72527|2000-08-02|2001-08-02|10002|72527|1999-08-03|2000-08-02
10002|72527|2000-08-02|2001-08-02|10002|72527|2000-08-02|2001-08-02
当进行where的限制,不进行group by 的时候,得到的数据结构如下:
SELECT * FROM salaries AS S1 INNER JOIN salaries AS s2 ON S1.emp_no = S2.emp_no WHERE S1.to_date = '9999-01-01';10001|88958|2002-06-22|9999-01-01|10001|85097|2001-06-22|2002-06-22
10001|88958|2002-06-22|9999-01-01|10001|88958|2002-06-22|9999-01-01