August 4, 2003 at 2:01 am
How can query from a table results based
on top 5 salaries and lowest 5 salaries.
Many thanks in advance and looking forward for the responses.
Jacobe
August 4, 2003 at 2:16 am
I'm not quite sure of the result you want, but you could try, for starters:
select * from
(
select top 5 with ties * from employee
order by salary desc
) top5
union
select * from
(
select top 5 with ties * from employee
order by salary
) bottom5
order by salary
Cheers,
- Mark
Cheers,
- Mark
August 4, 2003 at 2:28 am
Hello Mark
I tried with another query which is as follows
SELECT TOP 5 *
FROM table
However I am not getting the lowest 5. I tried with bottom, but it's not working
Thanks,
Jacobe
August 4, 2003 at 2:34 am
quote:
Hello MarkI tried with another query which is as follows
SELECT TOP 5 *
FROM table
However I am not getting the lowest 5. I tried with bottom, but it's not working
what about
ORDER BY salary (DESC) ?
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
August 4, 2003 at 2:40 am
Hello,
Like top 5, is there something called bottom 5 to fetch the lowest 5?
Thanks
Jacobe
August 4, 2003 at 2:43 am
Hi Jacobe,
quote:
Like top 5, is there something called bottom 5 to fetch the lowest 5?
to the best of my knowledge, there is no such keyword.
However, proper sorting yields the same result
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
August 4, 2003 at 2:44 am
Jacobe,
If you order by SALARY or SALARY ASC you'll get the lowest 5 salaries.
If you order by SALARY DESC you'll get the highest 5 salaries.
Cheers,
- Mark
Cheers,
- Mark
August 4, 2003 at 2:48 am
Hello,
Yes that's correct,but my query is bit complicated and I cannot use desc or asc.Nevertheless I am use 2 different queries to fetch records.
Thanks,
Jacobe
August 4, 2003 at 2:51 am
quote:
Yes that's correct,but my query is bit complicated and I cannot use desc or asc.Nevertheless I am use 2 different queries to fetch records.
would you mind posting the whole query?
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
August 4, 2003 at 3:08 am
Hello,
I woul like to do that, unfortunately, I am behind exchange server and hence cannot post any code here. I will try from my residence.
Thanks indeed,
Jacobe
August 4, 2003 at 3:10 am
quote:
I woul like to do that, unfortunately, I am behind exchange server and hence cannot post any code here. I will try from my residence.
does a simple copy and paste not work ???
Cheers,
Frank
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
August 4, 2003 at 9:45 am
Hi Marc,whatz with ties in the first query,what does it do.
August 4, 2003 at 10:45 am
Here's an example of what WITH TIES does...
DATA;
1
2
2
3
4
SELECT TOP 3
FROM data
ORDER BY numbers ASC
1
2
2
SELECT TOP 3 WITH TIES
FROM data
ORDER BY numbers ASC
1
2
2
3
Also, there isn't a BOTTOM # in SQL Server. You have to use the ORDER BY ..... DESC or ASC to get the proper order.
-SQLBill
August 4, 2003 at 3:06 pm
Hi SQLBill,
Actually, your example would return 1,2,2 in both cases.
An example that would demonstrate WITH TIES is only slightly different:
SELECT TOP 2 *
FROM data
ORDER BY numbers ASC
numbers
-----------
1
2
compared with:
SELECT TOP 2 WITH TIES *
FROM data
ORDER BY numbers ASC
numbers
-----------
1
2
2
Cheers,
- Mark
Cheers,
- Mark
August 5, 2003 at 7:18 am
Mark,
You're right.
I was SAD that day.
(Sleeping At Desk)
-SQLBill
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply