![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
For this schema:http://github.com/metaperl/DBIx--Sho...788f6d2825108c... How would you get the top 5 salaries (and their department at the time) such that the top 5 lists any employee only once? My fear with my current query is that the GROUP BY will arbitrarily reduce multiple instances of an employee to one row, without choosing the row with the highest salary: -- top 5 salaries, distinct by employees, and their department at the time SELECT * * FROM * salaries INNER JOIN dept_emp USING (emp_no) INNER JOIN departments USING (dept_no) WHERE * salaries.to_date <= dept_emp.to_date GROUP BY * emp_no ORDER BY * salary DESC LIMIT 5 ; |
#3
| |||
| |||
|
|
I'm not sure what GROUP BY emp_no is supposed to mean (in this context). |
|
create table T (x int not null, y int not null, primary key (x,y)); insert into T (x,y) values (1,1), (1,2); What would be the result of: select x, y from T group by x; |
|
Start by figuring out how to get the top 5 salaries from the salary table |
#4
| |||
| |||
|
|
SELECT * FROM salaries ORDER BY salary DESC LIMIT 5; but this might list the same employee multiple times... take Microsoft. Bill Gates might have 5 different salaries, but they might always the highest. I only want 1 salary from any particular employee. And I want it to be his highest. |
#5
| |||
| |||
|
|
SELECT emp_no, * * * * max(salary) FROM salaries GROUP BY emp_no ORDER BY 2 DESC LIMIT 5 |

![]() |
| Thread Tools | |
| Display Modes | |
| |