473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL Query - Please Help!!

I am looking for a SQL command that allows me to list for example the top 2
or 3 values for each category. for example I have a table with names, town,
salary. I want the names of the two people who have the highest salaries in
each town. Imagine the towns are Liverpool, Southport, London. the result
of query should give me two names for each town and the names, towns and
salaries must be listed. The listed names must be those who get the highest
salaries in each town. Looked in every book but not found it. I found a Top
command but didn't give me the correct results. Hope someone can help.
Thanks


Jul 20 '05 #1
1 1442
The problem with your specification is what to do when there isn't a
well-defined "top 2" or "top 3" or "top N" for any particular town. See
London in the example below. Which employees are the two highest paid in
London?

CREATE TABLE Employees (empid INTEGER PRIMARY KEY, empname VARCHAR(25) NOT
NULL UNIQUE, town VARCHAR(25) NOT NULL, salary NUMERIC(10,0) NOT NULL)

INSERT INTO Employees VALUES (1, 'Nancy Davolio', 'Liverpool', 54670)
INSERT INTO Employees VALUES (2, 'Andrew Fuller', 'Liverpool', 34570)
INSERT INTO Employees VALUES (3, 'Janet Leverling', 'Southport', 33550)
INSERT INTO Employees VALUES (4, 'Margaret Peacock', 'London', 51760)
INSERT INTO Employees VALUES (5, 'Steven Buchanan', 'Southport', 34530)
INSERT INTO Employees VALUES (6, 'Michael Suyama', 'London', 46500)
INSERT INTO Employees VALUES (7, 'Robert King', 'London', 46500)
INSERT INTO Employees VALUES (8, 'Laura Callahan', 'Liverpool', 23440)
INSERT INTO Employees VALUES (9, 'Anne Dodsworth', 'Southport', 45200)

Assuming you want to include all tied values in the result, giving *not less
than* two rows per town:

SELECT E1.empid, E1.empname, E1.town, E1.salary
FROM Employees AS E1
LEFT JOIN Employees AS E2
ON E1.town = E2.town AND E1.salary<E2.salary
GROUP BY E1.empid, E1.empname, E1.town, E1.salary
HAVING COUNT(E2.empid)<2
ORDER BY E1.town, E1.salary DESC

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
14
by: Bruce W...1 | last post by:
I do a query to MySQL using PHP. Well actually I do too many of them which is the problem, it's too slow. I think maybe an inner join or something would be better but I can't figure this out. ...
1
by: Hought, Todd | last post by:
Hi all, trying to run a query against a table, to pull the date out, and order it. problem is, the date is stored in character (string) format, not as an actual timestamp, so parsing it back into...
1
by: PMB | last post by:
Thank you in advance for any and all assistance. I'm trying to use a make table query to pull the last transactionID, so I can use an append query to reset the transactionID to the next...
5
by: Norma | last post by:
I am trying to make a query pull data from between the dates I enter in the parameter but also look back 'in time' to see where 2 other fields have null values, and only pull data into the query if...
10
by: Randy Harris | last post by:
I imported records into a table, later found out that many of them had trailing spaces in one of the fields. If I'd caught it sooner, I could have trimmed the spaces before the import. This...
2
by: schoultzy | last post by:
Hello Everyone, This is probably a simple fix so be kind when/if you reply. The query below retrieves information for individuals based on a column named ATTRIB_DEF, and assorted other columns;...
6
by: leeg | last post by:
Help please. Apologies but I am poor in access and programming :o) and am having trouble getting my head around this one!!...again!!! I need to have a query or report to flag up someone who has...
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: ----------------
1
by: write2ashokkumar | last post by:
hi... i have the table like this, Table Name : sample Total Records : 500000 (Consider like this) Sample Records: ----------------
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.