SQL Stats:

How to support the site


Site Wide Message: (current site time 8/1/2010 1:15:51 AM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

Finding No of Max values

Print
Email
VB icon
Submitted on: 12/17/2009 8:48:25 AM
By: Kiran K Surve 
Level: Advanced
User Rating: Unrated
Compatibility:Other

Users have accessed this code  2039 times.
 
author picture
(About the author)
 
     To finding no of max values from a table.max function gives only one value, but this procedure gives top max no of values.I have created this procedure in sybase. U can check the procedure using same logic in other databse also
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Finding No of Max values
-- Description:To finding no of max values from a table.max function gives only one value, but this procedure gives top max no of values.I have created this procedure in sybase. U can check the procedure using same logic in other databse also
-- By: Kiran K Surve
--
-- Inputs:it takes no as input
--
-- Returns:No. of rows retrieves with top max values as per input no.
--
-- Assumes:Create a table say original table with 2 columns, 1st col is unique & second columns contain max value column.
--
-- Side Effects:no side effect
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1338&lngWId=5--for details.--**************************************

-- the below procedure is developed in sybase 12.1
--the following procedure finds no of rows of max values as per requirement
--if one want to retrieve 25 no of maxvaluerows then procedure gets parameter 25 as input
create procedure no_maxfindinoriginal_table(@rrow int)
as
begin
declare @c int
select @c = 1
create table #temp
(col1 int,
 valuecol numeric(15,2))
create table #temp00
(col1 int,
 valuecol numeric(15,2))
insert into #temp
select Col1,valuecol
from original_table
while @c <= @rrow 
begin	
	insert into #temp00
	select Col1,valuecol 
	from #temp having valuecol = max(valuecol)
	
	delete from #temp
	where (convert(char(6),col1)+convert(char(17),valuecol)) in (select 
	(convert(char(6),col1)+convert(char(17),valuecol)) from #temp00)
	select @c = @c + 1 
end
select #temp00.col1, #temp00.valuecol from #temp00
end;

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.