dbTalk Databases Forums  

Getting the top 5 DISTINCT and making sure you got the highest

comp.databases comp.databases


Discuss Getting the top 5 DISTINCT and making sure you got the highest in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
metaperl
 
Posts: n/a

Default Getting the top 5 DISTINCT and making sure you got the highest - 08-12-2009 , 02:26 PM






For this schema:
http://github.com/metaperl/DBIx--Sho...MAX/schema.pdf

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 ;

If you want to download the sample database in SQLite format, here's
how to do so:
#!/bin/bash -x

export TGZ=sampledb.tgz

wget http://sampledb.svn.sourceforge.net/...ar.gz?view=tar
mv sampledb.tar.gz* $TGZ
tar xvfz $TGZ

Reply With Quote
  #2  
Old   
Lennart
 
Posts: n/a

Default Re: Getting the top 5 DISTINCT and making sure you got the highest - 08-13-2009 , 12:43 AM






On 12 Aug, 21:26, metaperl <metap... (AT) gmail (DOT) com> wrote:
Quote:
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 ;

I'm not sure what GROUP BY emp_no is supposed to mean (in this
context). Assume the following table:

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


/Lennart

Reply With Quote
  #3  
Old   
Terrence Brannon
 
Posts: n/a

Default Re: Getting the top 5 DISTINCT and making sure you got the highest - 08-13-2009 , 08:34 AM



On Aug 13, 1:43*am, Lennart <Erik.Lennart.Jons... (AT) gmail (DOT) com> wrote:

Quote:
I'm not sure what GROUP BY emp_no is supposed to mean (in this
context).
An employee can have many salaries in the salaries table. I only care
about one salary per employee, so I did a GROUP BY. But because I care
about the highest salary of an employee, I am not sure which salary
row will be retained in the GROUP BY employee_id

Assume the following table:
Quote:
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;
Thats just the point. I'm not sure I have a guarantee on that. And I
know that I want the (1,2) row, not the (1,1) row.


Quote:
Start by figuring out how to get the top 5 salaries from the salary
table
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.

Reply With Quote
  #4  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Getting the top 5 DISTINCT and making sure you got the highest - 08-13-2009 , 09:08 AM



Terrence Brannon wrote on 13.08.2009 15:34:
Quote:
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.
SELECT emp_no,
max(salary)
FROM salaries
GROUP BY emp_no
ORDER BY 2 DESC
LIMIT 5

Reply With Quote
  #5  
Old   
Terrence Brannon
 
Posts: n/a

Default Re: Getting the top 5 DISTINCT and making sure you got the highest - 08-13-2009 , 03:14 PM



On Aug 13, 10:08*am, Thomas Kellerer <OTPXDAJCS... (AT) spammotel (DOT) com>
wrote:

Quote:
SELECT emp_no,
* * * * max(salary)
FROM salaries
GROUP BY emp_no
ORDER BY 2 DESC
LIMIT 5
Ok, thanks a lot. I'd forgotten about that aspect of GROUP BY

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.