NEWS

Monday, May 13, 2013

Find Nth Maximum value


"How do I find Nth maximum value?" is one of the most asked questions

Here are some methods 
I explain how to find 5th Maximum value
01.declare  @number table(num integer
02. 
03.Insert into @number values(3456)
04.Insert into @number values(846)
05.Insert into @number values(1456)
06.Insert into @number values(3098)
07.Insert into @number values(34)
08.Insert into @number values(67856)
09.Insert into @number values(906)
10.Insert into @number values(34656)
11.Insert into @number values(9056)
12.Insert into @number values(3036)
(1) Use Inner Join
1.select t1.num from @number t1 inner join @number t2 on t1.num<=t2.num
2.group by t1.num having count(t1.num)=5
(2) Use Top Operator
1.Select top 1 num from
2.(
3.Select top 5 num from @number order by num desc
4.) T
5.order by num asc
(3) Generate Serial No based on descending order of the
1.select num from
2.(
3.Select (select count(*) from @number where num>=T.num)as Sno ,num
4.from @number as T
5.as temp
6.where Sno=5
(4) Generate Serial No based on descending order of the values in where Clause
1.select num from @number as n
2.where (select count(*) from @number where num>=n.num)=5
(5) Use Aggregate
1.Select min(num) from (select top 5 num from @number order by num desc) T
(6) Use Row_number() function
1.select num from
2.(
3.select num, row_number() over (order by num descas sno from @number
4.as t
5.where sno=5
6. 
7.Now you can replace to the number that you want to find the maximum value


General SQL query should be as follows

select sal from emp a where N = (select count(distinct sal) from emp b where a.sal <= b.sal) where N = any value.

14 comments:

  1. Fastidious respond in return of this matter with firm arguments and explaining the whole thing regarding that.


    Check out my web page :: recycling facts

    ReplyDelete
  2. I used to be able to find good info from your blog posts.



    Here is my blog :: http://goang.alfahosting.org

    ReplyDelete
  3. Pretty nice post. I simply stumbled upon your blog and wished
    to mention that I've truly enjoyed browsing your weblog posts. In any case I will be subscribing for your feed and I am hoping you write again soon!

    My blog post :: dry scalp treatments

    ReplyDelete
  4. You really make it seem so easy with your presentation but
    I find this topic to be really something which I think I would never understand.
    It seems too complicated and very broad for me. I'm looking forward for your next post, I'll try to get the hang of it!


    Check out my site :: diarrhea remedies

    ReplyDelete
  5. Hey! I simply noticed one other message in another blog
    that appeared like this. How have you learnt all this stuff?
    That’s one cool post.

    My homepage: having trouble getting pregnant at 40

    ReplyDelete
  6. Excellent write-up. I absolutely appreciate this site.
    Keep it up!

    my web-site :: new cellulite treatment

    ReplyDelete
  7. We are a gaggle of volunteers and starting a new scheme in our community.
    Your site offered us with helpful info to work on.
    You have done a formidable job and our entire group will
    be grateful to you.

    Look into my web site: Psn Code Generator

    ReplyDelete
  8. Hey there, I think your blog might be having browser compatibility issues.

    When I look at your blog in Firefox, it looks fine but when opening in Internet Explorer, it has some
    overlapping. I just wanted to give you a quick heads up!
    Other then that, awesome blog!

    my webpage Louis Vuitton Outlet

    ReplyDelete
  9. Hi there exceptional website! Does running a blog like this require a massive amount work?
    I have virtually no expertise in programming however I had been hoping to start
    my own blog soon. Anyway, if you have any recommendations or tips for new blog owners please
    share. I understand this is off subject but I simply needed to ask.

    Cheers!

    my site - Natural Cleanse Diet

    ReplyDelete
  10. Just desire to say your article is as amazing.

    The clearness for your post is simply spectacular and i can think you're knowledgeable in this subject. Fine with your permission let me to seize your RSS feed to keep updated with impending post. Thanks 1,000,000 and please keep up the enjoyable work.

    Here is my web blog: Microsoft Office Gratuit

    ReplyDelete
  11. I do not even know how I ended up here, but I thought this post was great.
    I do not know who you are but certainly you are going to a famous blogger
    if you aren't already ;) Cheers!

    Also visit my blog post :: Home income kit reviews

    ReplyDelete
  12. Good day! I know this is kind of off topic but I was
    wondering which blog platform are you using for this site?
    I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

    My web-site :: payday loans direct lenders

    ReplyDelete