Article View: bit.databases.mssql-l
Article #19Re: SQL Query - please help!!!
From: Aaron W. West
Date: Sun, 30 May 2004 09:37
Date: Sun, 30 May 2004 09:37
78 lines
3896 bytes
3896 bytes
Your approach is probably best. But let me see if I can come up with a
single query that can do it...using correlated subqueries. Okay, I did, and
it worked with some data of mine, but don't want to reproduce my query and
outputs here.
Try this:
select town, name, salary
from emp e1
where salary >=
(select min(salary) from emp e2
where e1.town = e2.town
and 2 >= (select count(*) from emp e3
where e3.town = e2.town
and e3.salary >= e2.salary)
)
order by town, salary desc, emp
Translation:
Find the employees
whose salary is greater than or equal to
the minimum of salaries in this town
out of the salaries where there are no more than 2
salaries in this town greater than or equal to that salary
Note that if two employees in the town have the same salary, there will be 3
or more rows returned for the town.
I've just recently started to learn the power of subqueries for things like
this. But often they are slow; the approach mentioned below is probably
faster and may be more advisable.
You could also rank the salaries, if you have a column to store the ranks
in.
Very MS SQL Server-specific, a technique I just learned:
alter table emp add rank int
declare @rank int
declare @town varchar(30)
set @rank=0
set @town=''
create clustered index Xemp on emp(town,salary desc)
update emp set rank=@rank=case
when town<>@town then 1 else @rank+1 end, @town=town
from emp
Then you just select those with ranks of 2 or less.
---
"Robert Djabarov" <Robert.Djabarov@iccs-consulting.com> wrote in message
news:sHPrc.6168$be.809@newsread2.news.pas.earthlink.net...
You need to create a function that would return TOP 2 or TOP 3, and then
link this function with your CategoryMaster table.
"ComputerMan" <talktome@btinternet.com> wrote in message
news:c0m8fq$jks$1@hercules.btinternet.com...
> 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
>
>
>
Technical Details
Message-ID:
<jKednblZO_fPNiTd38DK-g@speakeasy.net>Path:
arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mailReferences:
<c0m8fq$jks$1@hercules.btinternet.com> <sHPrc.6168$be.809@newsread2.news.pas.earthlink.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f6611,ddad40cdb9245699 X-Google-Attributes: gidf6611,public Path: arc1-ca.pugleaf.net!controlnews3.google.com!news1.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.speakeasy.net!news.speakeasy.net.POSTED!not-for-mail NNTP-Posting-Date: Sun, 30 May 2004 04:37:22 -0500 From: "Aaron W. West" <tallpeak@hotmail.NO.SPAM> Newsgroups: bit.databases.mssql-l References: <c0m8fq$jks$1@hercules.btinternet.com> <sHPrc.6168$be.809@newsread2.news.pas.earthlink.net> Subject: Re: SQL Query - please help!!! Date: Sun, 30 May 2004 02:37:19 -0700 X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 Message-ID: <jKednblZO_fPNiTd38DK-g@speakeasy.net> Lines: 81 NNTP-Posting-Host: 216.231.49.82 X-Trace: sv3-qXxdJQ3GBKOILYU0WH3uBj1rA1uRmC5uY4wCUg0J0u1KzJkse0pJ0EJmQK63yP59DI0E6Yq6SRrxQtK!XGrjGgVUkpfjmM6m5RC/XThR+0w1G7rvR+lVma2bYOyqlDF/Tfh/zqn2BxOgDY7TDgC/ShkSv02f!FoCqP5iRT1R5Z19SkJvi X-Complaints-To: abuse@speakeasy.net X-DMCA-Complaints-To: abuse@speakeasy.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.1