473,800 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return highest value in recordset

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
Jul 19 '05 #1
13 2305
SELECT TOP 10 ....
Jul 19 '05 #2
shank wrote on 25 jun 2004 in microsoft.publi c.inetserver.as p.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)
Jul 19 '05 #3

"shank" <sh***@tampabay .rr.com> wrote in message news:OZ******** ********@tk2msf tngp13.phx.gbl. ..
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.
Jul 19 '05 #4
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("Pric e").Value > MaxValue Then
MaxValue = RS.Fields("Pric e").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(priceColum n,i) > MaxValue Then
MaxValue = Rows(priceColum n,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.
Jul 19 '05 #5
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.
Jul 19 '05 #6
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 your
query ] ... correct?
Where am I going wrong?
thanks
<%
Rows = rsShowrecords.G etRows(), 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 query ]
For i = 0 To UBound(Rows,2)
If Rows(priceColum n,i) > MaxValue Then
MaxValue = Rows(priceColum n,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...

Jul 19 '05 #7
> 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.)
Jul 19 '05 #8
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.G etRows(), 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]" <te*****@dnartr eb.noraa> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
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.)

Jul 19 '05 #9
shank wrote:
<%
Rows = rsShowrecords.G etRows(), 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"
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4815
by: Adrian Parker | last post by:
Hi. I would like to query a database, given several where clauses to refine my search, and return the value of one single field in the database. eg: I have a table that lists teachers. Their first name, last name, age, unique teacher number, etc is in the file. I want to return the unique teacher number, for example, of the teacher whose first name is Jane and last name is Doe. How does one do this?
7
5732
by: JT | last post by:
how can i see a stored procedures return value in ASP??
2
2300
by: Megan | last post by:
Hi everybody- I have 2 tables, Hearings and Rulings. Both have primary keys called, CaseID, that are autonumbers. I don't want both tables to have the same autonumber. For example, if Hearings has 55, then I want Rulings to have a number greater than 55. Two or more Hearings may be entered before a Ruling is entered. For example, Hearings with CaseIDs= 55, 56, and 57 may be entered before a Ruling or vice versa. There is no definite...
21
13487
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the second pass we can find the highest number which is less than the number we obtained in the 1st pass.
7
3358
by: Jan | last post by:
Hi there, Is there a fast way to get the highest value from an array? I've got the array strStorage(intCounter) I tried something but it all and's to nothing If someone good helpme, TIA
3
8814
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have an int array and was just wondering if there is a way to get the highest value in the array? for instance, int myValues = new int { 0, 1, 2 } highest value is 2. thanks,
1
3774
by: Coll | last post by:
Hi - I'm trying to figure out how to append a record to a table and then open a form and display that record. My thought was to use the autonum primary key field (recordnum) and display the highest value (which would be the new record), but I'm not sure if that can be done. Any thought on displaying the record with the highest value in the recordnum field or another approach would be appreciated. Thanks!
8
11264
by: colmkav | last post by:
Can someone tell me how I can access the return value of a function called from Oracle as opposed to a store proc from oracle? my oracle function is get_num_dates_varposfile. I am only used to using this method with store procs that dont return a value back to Access. Hope this makes sense. Set Cmd = New Command With Cmd Set .ActiveConnection = get_XE_Conn 'makes a connection Oracle XE
12
5144
by: Dooza | last post by:
I have a stored procedure that takes a number of inputs, does a bulk insert, and then outputs a recordset. When I run the stored procedure in Server Management Studio I also get a return value from the stored procedure which is an INT. I want to access this return value on my ASP/VBScript page, but do not know how to access it. Here is my code so far:
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.