Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


find max


find max

Author
Message
raghuldrag
raghuldrag
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 473
hi friends,

create table emp(ename varchar(34),job char(33),sal varchar(22))
insert into emp values('sa','sales','400')
insert into emp values('ka','manager','4000')
insert into emp values('sam','Markting','2500')
insert into emp values('sabu','system','3900')



in these table model i ve a report now i need to display the 3rd max salry peoples,......like ... when i am entering postion its display the tat postion of max salry..... ve write on single query
vinu512
vinu512
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1149 Visits: 1618
From what I understand, this should do it:


Declare @position Char(33)
Set @position = 'sales'
Select ename, job From
(
Select *, ROW_NUMBER() Over (Partition By job Order By Sal) As rn From Ex
) As a
Where job = @position AND rn = 3



I hope this is what you are looking for.

Vinu Vijayan

For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden ;-)
rhythmk
rhythmk
Say Hey Kid
Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)Say Hey Kid (662 reputation)

Group: General Forum Members
Points: 662 Visits: 1062
raghuldrag (8/17/2012)
hi friends,

create table emp(ename varchar(34),job char(33),sal varchar(22))
insert into emp values('sa','sales','400')
insert into emp values('ka','manager','4000')
insert into emp values('sam','Markting','2500')
insert into emp values('sabu','system','3900')



in these table model i ve a report now i need to display the 3rd max salry peoples,......like ... when i am entering postion its display the tat postion of max salry..... ve write on single query


Please use Dense rank on sal for this.

--rhythmk
------------------------------------------------------------------
To post your question use below link

http://www.sqlservercentral.com/articles/Best+Practices/61537/
:-)
R.P.Rozema
R.P.Rozema
Mr or Mrs. 500
Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)Mr or Mrs. 500 (517 reputation)

Group: General Forum Members
Points: 517 Visits: 1681
Also, use proper types for your columns. Numeric values -like a salary- should not be entered as strings. If you store them as strings the values may not sort as you would expect. For example:
select max(salary)
from (
select '20' as salary
union all select '100'
) t


The output of this is:

----
20

(1 row(s) affected)


Probably not what you wanted, is it?



Posting Data Etiquette - Jeff Moden
Posting Performance Based Questions - Gail Shaw
Hidden RBAR - Jeff Moden
Cross Tabs and Pivots - Jeff Moden
Catch-all queries - Gail Shaw


If you don't have time to do it right, when will you have time to do it over?
SomewhereSomehow
SomewhereSomehow
SSC Journeyman
SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)SSC Journeyman (96 reputation)

Group: General Forum Members
Points: 96 Visits: 469
vinu512 (8/17/2012)
From what I understand, this should do it:


Declare @position Char(33)
Set @position = 'sales'
Select ename, job From
(
Select *, ROW_NUMBER() Over (Partition By job Order By Sal) As rn From Ex
) As a
Where job = @position AND rn = 3



I hope this is what you are looking for.

Just one note. If we are searching max there should be "Order By Sal desc". I think it's a mistype.

raghuldrag
Consider the case where there will be no more than 2 persons on postion. Should we loose them, or not?
If not, so maybe there is better to use: rn <=3, and than top(1)...order by rn desc.
Also, why are you defining number column like varchar datatype? There might be a problem, because strings sorted in another way than numbers.


I am really sorry for my poor gramma. And I hope that value of my answers will outweigh the harm for your eyes.
Blog: http://somewheresomehow.ru
Twitter: @SomewereSomehow
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
rhythmk (8/17/2012)
raghuldrag (8/17/2012)
hi friends,

create table emp(ename varchar(34),job char(33),sal varchar(22))
insert into emp values('sa','sales','400')
insert into emp values('ka','manager','4000')
insert into emp values('sam','Markting','2500')
insert into emp values('sabu','system','3900')



in these table model i ve a report now i need to display the 3rd max salry peoples,......like ... when i am entering postion its display the tat postion of max salry..... ve write on single query


Please use Dense rank on sal for this.


Agreed that you should look at RANK() and DENSE_RANK() and decide which to use based on the business rules for tie breakers.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
raghuldrag
raghuldrag
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 473
not working the ROW_NUMBER(), and DENSE_RANK() function in server 2000
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
raghuldrag (8/17/2012)
not working the ROW_NUMBER(), and DENSE_RANK() function in server 2000


Correct. They are SQL 2005 features and this is a SQL 2008 forum.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
raghuldrag
raghuldrag
SSC-Enthusiastic
SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)SSC-Enthusiastic (186 reputation)

Group: General Forum Members
Points: 186 Visits: 473
need the output in sql server 2000 friends
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4249 Visits: 6431
Operating only with stone knives and bearskins at my disposal, I offer you this:


create table #emp(ename varchar(34),job char(33),sal varchar(22))
insert into #emp values('sa','sales','400')
insert into #emp values('ka','manager','4000')
insert into #emp values('sam','Markting','2500')
insert into #emp values('sabu','system','3900')

SELECT TOP 3 *
FROM #emp
ORDER BY CAST(sal AS INT) DESC

DROP TABLE #emp




However if there's a tie for the #3 spot, one will get left out. You might want to check to see if using TOP PERCENT might work better for your case.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search