return highest value in recordset
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Web Servers reviews > IIS server support > IIS ASP > return highest value in recordset




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    return highest value in recordset  
shank


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

How do you return the highest value in a recordset of maybe 100 records?
Is it necessary to run 2 recordsets?
I was hoping it was as simple as Max([Price]), but no luck.

thanks







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
Maarten


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

SELECT TOP 10 ....







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
Evertjan.


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

shank wrote on 25 jun 2004 in microsoft.public.inetserver.asp.general:

> How do you return the highest value in a recordset of maybe 100 records?
> Is it necessary to run 2 recordsets?
> I was hoping it was as simple as Max([Price]), but no luck.

if you mean a database:

Select top 1 myField, myOtherField from myTable order by myField Desc

or

Select max(myField) as higest from myTable

(if you only want that value)


Not tested, so I could be completely wrong.



--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)





[ Post a follow-up to this message ]



    Re: return highest value in recordset  
Al Reid


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM


"shank" <shank@tampabay.rr.com> wrote in message news:OZW%23O7tWEHA.3944@tk2msftngp13.phx.gb
l...
> How do you return the highest value in a recordset of maybe 100 records?
> Is it necessary to run 2 recordsets?
> I was hoping it was as simple as Max([Price]), but no luck.
>
> thanks
>
>

If  you want the entire record try:

SELECT * FROM tbl WHERE fld = (SELECT MAX(fld) FROM tbl)

Better yet, only select the fields you really need.







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
Dave Anderson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

shank wrote:
> How do you return the highest value in a recordset of maybe 100
> records? Is it necessary to run 2 recordsets?
> I was hoping it was as simple as Max([Price]), but no luck.

Define "highest value" in something that is usually multi-dimensional, with
multiple data formats.

There are probably several solutions, and you don't really provide enough
detail for me to recommend one over another. Here's one:

While NOT RS.EOF
If RS.Fields("Price").Value > MaxValue Then
MaxValue = RS.Fields("Price").Value
End If

..other processing...

Call RS.MoveNext()
Wend

Even better, if you have used GetRows, you don't have to worry about cursor
types, and you can do all kinds of things to the data:

Rows = RS.GetRows(), priceColumn = [ integer: depends on your query ]
For i = 0 To UBound(Rows,2)
If Rows(priceColumn,i) > MaxValue Then
MaxValue = Rows(priceColumn,i)
End If
Next

Note that this can be done at any time, since it's in an array. No need to
worry about cursor types, and no need to leave connections or recordsets
open. On the other hand...

If your query is ordered on Price, just grab the first (DESC ordered query)
or last (ASC ordered one) value in the recordset.

If you don't mind a second recordset, you can use this approach:

SELECT TOP 1 Price
FROM MyTable
WHERE [ same conditions as earlier query ]
ORDER BY Price DESC



As I said, options abound.


--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
Dave Anderson


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

Al Reid wrote:
>
> SELECT * FROM tbl WHERE fld = (SELECT MAX(fld) FROM tbl)

That reminds me -- one can always return it as a column in the recordset:

DECLARE @MaxPrice DECIMAL(9,2)
SELECT @MaxPrice = MAX(Price) FROM myTable

SELECT *, @MaxPrice AS MaxPrice
FROM myTable
WHERE [ your conditions ]


MaxPrice will be in every row. Not the most efficient way, but not
necessarily a bad one.



--
Dave Anderson

Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
shank


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

I wanted to try the GetRows() method and I'm not having much luck. Actually,
"the page cannot be displayed".
I assumed you wanted some integer like 100 for [ integer: depends on you
r
query ] ... correct?
Where am I going wrong?
thanks
<%
Rows = rsShowrecords.GetRows(), Price = 100
For i = 0 To UBound(Rows,2)
If Rows(Price,i) > MaxValue Then
MaxValue = Rows(Price,i)
End If
Next
%>

Highest Price: <%=MaxValue%>
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
= =
> Even better, if you have used GetRows, you don't have to worry about
cursor
> types, and you can do all kinds of things to the data:
>
>     Rows = RS.GetRows(), priceColumn = [ integer: depends on your quer
y ]
>     For i = 0 To UBound(Rows,2)
>         If Rows(priceColumn,i) > MaxValue Then
>             MaxValue = Rows(priceColumn,i)
>         End If
>     Next
>
> Note that this can be done at any time, since it's in an array. No need to
> worry about cursor types, and no need to leave connections or recordsets
> open. On the other hand...







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
Aaron [SQL Server MVP]


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

> I wanted to try the GetRows() method and I'm not having much luck.
Actually,
> "the page cannot be displayed".

http://www.aspfaq.com/2109

--
http://www.aspfaq.com/
(Reverse address to reply.)







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
shank


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

I had Show friendly errors unchecked before. I'm on a shared server and I
contacted my host. They cannot change anything that would reflect a more
descriptive error. So I'm back to "what's wrong with this?"
My recordset is: rsShowrecords
The price column is: Price
I'm placing the below code after the recordset.
What am I missing?
thanks!

<%
Rows = rsShowrecords.GetRows(), Price = 100
For i = 0 To UBound(Rows,2)
If Rows(Price,i) > MaxValue Then
MaxValue = Rows(Price,i)
End If
Next
%>

Highest Price: <%=MaxValue%>
--------------------------------------------------------
--------------------------------------------------------

"Aaron [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:%235a9ZrvWEHA.2520@TK2MSFTNGP12.phx.gbl... 
> Actually, 
>
> http://www.aspfaq.com/2109
>
> --
> http://www.aspfaq.com/
> (Reverse address to reply.)
>
>







[ Post a follow-up to this message ]



    Re: return highest value in recordset  
Bob Barrows [MVP]


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-26-04 03:19 PM

shank wrote:
> <%
> Rows = rsShowrecords.GetRows(), Price = 100

This is a single line of code???

Why would you think this could work? You're trying to do two things in a
single statement??


--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:33 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register