non-equi join更好理解的方法
统计salary的累计和running_total
http://www.nowcoder.com/questionTerminal/58824cd644ea47d7b2b670c506a159a6
window function大家可能都能明白,也很好用。我看到大神有种用non-equi join的方法也可以得解,而且很普遍。
SELECT s1.emp_no, s1.salary, (SELECT SUM(s2.salary) FROM salaries s2 WHERE s2.emp_no <= s1.emp_no AND s2.to_date = '9999-01-01') FROM salaries s1 WHERE s1.to_date = '9999-01-01' ORDER BY s1.emp_no;
其实我是不太理解这种方法的,是如何直接在select里面就取出来值,欢迎大佬来解释一下,我也用non-equi的方法写出了一个解,更加好理解一点,发出来和大家交流一下。
SELECT s1.emp_no, s1.salary, SUM(s2.salary) AS running_total FROM salaries s1, salaries s2 WHERE s1.emp_no >= s2.emp_no AND s1.to_date = '9999-01-01' AND s2.to_date = '9999-01-01' GROUP BY s1.emp_no ORDER BY s1.emp_no;