473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

mysql query to produce the max value < 8411?

I tried using this query:

select max(revenue), bonus
from bonuses
where revenue < 8411
group by revenue

And it produces more than one row, which is what I don't want; I want only
one row, the row with the maximum revenue < 8411 (in this case, usually a
variable of course) without having to go through a rather lengthy table
query, parsing through it and finding the max.

Anyone out there know a cool mySQL or SQL trick to ensure I can only have
one row?

Thanx
Phil
Jul 17 '05 #1
9 6017
Phil Powell wrote:
I tried using this query:

select max(revenue), bonus
from bonuses
where revenue < 8411
group by revenue

And it produces more than one row, which is what I don't want; I want
only one row, the row with the maximum revenue < 8411 (in this case,
usually a variable of course) without having to go through a rather
lengthy table query, parsing through it and finding the max.

Anyone out there know a cool mySQL or SQL trick to ensure I can only
have one row?


Look up SQL syntax for HAVING vs WHERE....

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #2
Ok using this as my reference:

http://www.mysql.com/doc/en/SELECT.html

I came up with this query:

SELECT max( nnet_table_bonus_revenue ) AS revenue, nnet_table_bonus
FROM nnet_table_bonus
GROUP BY nnet_table_bonus_revenue
HAVING revenue < 8411

The query, however, still produced multiple results when I only want 1 row
returned :(

Phil
"127.0.0.1" <newsgroup(at)cr*********@verisign-sux-ijlkl.com> wrote in
message news:SG********************@news-server.bigpond.net.au...
Phil Powell wrote:
I tried using this query:

select max(revenue), bonus
from bonuses
where revenue < 8411
group by revenue

And it produces more than one row, which is what I don't want; I want
only one row, the row with the maximum revenue < 8411 (in this case,
usually a variable of course) without having to go through a rather
lengthy table query, parsing through it and finding the max.

Anyone out there know a cool mySQL or SQL trick to ensure I can only
have one row?


Look up SQL syntax for HAVING vs WHERE....

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>

Jul 17 '05 #3
Phil Powell wrote:
The query, however, still produced multiple results when I only want
1 row returned :(


MMmmm.... I think subqueries might be needed ... but I'm trying to get
away without them.

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #4
From what I understand, subqueries can't be done in older versions of mySQL,
am I correct? this is mySQL 3.23.41 on the remote site.

Phil

"127.0.0.1" <newsgroup(at)cr*********@verisign-sux-ijlkl.com> wrote in
message news:z9********************@news-server.bigpond.net.au...
Phil Powell wrote:
The query, however, still produced multiple results when I only want
1 row returned :(


MMmmm.... I think subqueries might be needed ... but I'm trying to get
away without them.

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>

Jul 17 '05 #5
If you're trying to find MAX(revenue) for columns with the same bonus you
have to use GROUP BY syntax, but i assume you're trying to find a record
with only maximum revenue and with appropriate bonus.

This will work fine:

SELECT
revenue, bonus
FROM bonuses
WHERE
revenue < 8411
ORDER BY revenue DESC
LIMIT 1

I tried using this query:

select max(revenue), bonus
from bonuses
where revenue < 8411
group by revenue

And it produces more than one row, which is what I don't want; I want only
one row, the row with the maximum revenue < 8411 (in this case, usually a
variable of course) without having to go through a rather lengthy table
query, parsing through it and finding the max.

Anyone out there know a cool mySQL or SQL trick to ensure I can only have
one row?

Thanx
Phil

Jul 17 '05 #6
just thinking, if you sort by revenue, the first post would be the one with
the highest value ?

"Phil Powell" <so*****@erols.com> wrote in message
news:Riqgb.53214$sp2.2301@lakeread04...
From what I understand, subqueries can't be done in older versions of mySQL, am I correct? this is mySQL 3.23.41 on the remote site.

Phil

"127.0.0.1" <newsgroup(at)cr*********@verisign-sux-ijlkl.com> wrote in
message news:z9********************@news-server.bigpond.net.au...
Phil Powell wrote:
The query, however, still produced multiple results when I only want
1 row returned :(


MMmmm.... I think subqueries might be needed ... but I'm trying to get
away without them.

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>


Jul 17 '05 #7
Jxrn-Inge Kristiansen wrote:
just thinking, if you sort by revenue, the first post would be the
one with the highest value ?


But not the highest value lower than 8411 ....

Maybe sort by abs(revenue-8411) descending (-:

--
Spam:newsgroup(at)cr*********@verisign-sux-klj.com
EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #8

"Phil Powell" <so*****@erols.com> schrieb im Newsbeitrag
news:Pypgb.52847$sp2.41639@lakeread04...
I tried using this query:

select max(revenue), bonus
from bonuses
where revenue < 8411
group by revenue

And it produces more than one row, which is what I don't want; I want only
one row, the row with the maximum revenue < 8411 (in this case, usually a
variable of course) without having to go through a rather lengthy table
query, parsing through it and finding the max.

Anyone out there know a cool mySQL or SQL trick to ensure I can only have
one row?

Thanx
Phil


SELECT revenue FROM bonuses WHERE revenue < 8411 ORDER BY revenue DESC LIMIT
1

HTH
Markus
Jul 17 '05 #9
That was exactly it! Spasibo!

Phil

"Dmitry Ruban" <di**@region35.ru> wrote in message
news:bl**********@gavrilo.mtu.ru...
If you're trying to find MAX(revenue) for columns with the same bonus you
have to use GROUP BY syntax, but i assume you're trying to find a record
with only maximum revenue and with appropriate bonus.

This will work fine:

SELECT
revenue, bonus
FROM bonuses
WHERE
revenue < 8411
ORDER BY revenue DESC
LIMIT 1

I tried using this query:

select max(revenue), bonus
from bonuses
where revenue < 8411
group by revenue

And it produces more than one row, which is what I don't want; I want only one row, the row with the maximum revenue < 8411 (in this case, usually a variable of course) without having to go through a rather lengthy table
query, parsing through it and finding the max.

Anyone out there know a cool mySQL or SQL trick to ensure I can only have one row?

Thanx
Phil


Jul 17 '05 #10

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

Similar topics

3
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my...
6
by: phpguy | last post by:
Hi people im making an online booking system as a final year project in my university i get this error which i cannot seem to fix ! Here is my code: PART 1...
0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
1
by: Cern | last post by:
Is it somebody out there who has made a migration from an Oracle server to an MySQL server?? The scenario is as simply: I've got a Oracle 8 server with a database with content that I want to...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.