473,406 Members | 2,954 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,406 software developers and data experts.

selecting the minimum of seven fields

Hi Gurus

I am new to mysql. I need to select the lowest number from seven fields
where the value is greater than 0. Can someone tell me how to do this.

So far, I have

Select RF1, RU1, ItemName FROM TABLE UNION
Select RF2, RU2, ItemName FROM TABLE UNION
Select RF3, RU3, ItemName FROM TABLE UNION
Select RF4, RU4, ItemName FROM TABLE UNION
Select RF5, RU5, ItemName FROM TABLE UNION
Select RF6, RU6, ItemName FROM TABLE UNION
Select RF7, RU7, ItemName FROM TABLE;

This will give me one long list with all the rate from (RF#) and Rate UNtil
(RU#). Now how do I select the lowest one for each itemname?

TIA

- Nicolaas
Jul 23 '05 #1
5 2356
windandwaves wrote:
Hi Gurus

I am new to mysql. I need to select the lowest number from seven fields
where the value is greater than 0. Can someone tell me how to do this.

So far, I have

Select RF1, RU1, ItemName FROM TABLE UNION
Select RF2, RU2, ItemName FROM TABLE UNION
Select RF3, RU3, ItemName FROM TABLE UNION
Select RF4, RU4, ItemName FROM TABLE UNION
Select RF5, RU5, ItemName FROM TABLE UNION
Select RF6, RU6, ItemName FROM TABLE UNION
Select RF7, RU7, ItemName FROM TABLE;

This will give me one long list with all the rate from (RF#) and Rate UNtil
(RU#). Now how do I select the lowest one for each itemname?

TIA

- Nicolaas

Nicolaas,

The quick band-aid approach to your question can be found in
the manual. Just look up the MIN function. Also, you only need
one SQL statement to solve your problem.

The long term solution is to learn SQL. Quickest way to
start is to use an on-line tutorial.

HTH
Jerry
Jul 23 '05 #2
jerry gitomer wrote:
windandwaves wrote:
Hi Gurus

I am new to mysql. I need to select the lowest number from seven
fields where the value is greater than 0. Can someone tell me how to
do this. So far, I have

Select RF1, RU1, ItemName FROM TABLE UNION
Select RF2, RU2, ItemName FROM TABLE UNION
Select RF3, RU3, ItemName FROM TABLE UNION
Select RF4, RU4, ItemName FROM TABLE UNION
Select RF5, RU5, ItemName FROM TABLE UNION
Select RF6, RU6, ItemName FROM TABLE UNION
Select RF7, RU7, ItemName FROM TABLE;

This will give me one long list with all the rate from (RF#) and
Rate UNtil (RU#). Now how do I select the lowest one for each
itemname? TIA

- Nicolaas

Nicolaas,

The quick band-aid approach to your question can be found in
the manual. Just look up the MIN function. Also, you only need
one SQL statement to solve your problem.

The long term solution is to learn SQL. Quickest way to
start is to use an on-line tutorial.

HTH
Jerry


Hi Jerry

Thank you jerry.

I know SQL pretty well, I just could not find the MIN function anywhere on
the mysql website. But that does not really solve my problem as such.

You dont actually have to use the min function, you can actually use

SELECT itemname, RF1 FROM TABLE GROUP BY itemname ORDER BY RF1;
max is: ORDER BY RU1 DESC

Anyway, the hard part is having 7 fields. I actually solved the problem, but
I have about four SQL statements, I was wondering if anyone knew any
shortcuts

Thank you once more for your reply.

- Nicolaas
Jul 23 '05 #3
windandwaves wrote:
Hi Gurus

I am new to mysql. I need to select the lowest number from seven fields
where the value is greater than 0. Can someone tell me how to do this.
MySQL includes a function LEAST() which takes a variable number of
arguments and gives back the least value.

e.g. LEAST(1, 2, 99, 5) returns 1.

See http://dev.mysql.com/doc/mysql/en/co...operators.html
This will give me one long list with all the rate from (RF#) and Rate UNtil
(RU#). Now how do I select the lowest one for each itemname?


Do you want the lowest RF# or the lowest RU# or some other computation
of them? Your other solution you posted makes me thing that you want
the lowest RF#.

Then you have to do some tricks to make sure if one of the values is 0
it won't become the least result. One method might be to force 0 (or
NULL) values to some artificial value much higher than any legitimate
value for any RF field.

SELECT LEAST(
IF(RF1,RF1,9999),
IF(RF2,RF2,9999),
IF(RF3,RF3,9999),
IF(RF4,RF4,9999),
IF(RF5,RF5,9999),
IF(RF6,RF6,9999),
IF(RF7,RF7,9999)), ItemName
FROM `Table`

I'm also inferring from your example that ItemName is a unique field,
i.e., there is only one row in Table per ItemName.

PS: The "seven fields" thing makes me wonder if you're going to have
more than seven fields in the future. Should this be better implemented
as a dependent table? It'd probably be easier to find the MIN() for
each ItemName that way too.

Regards,
Bill K.
Jul 23 '05 #4
Bill Karwin wrote:
windandwaves wrote:
Hi Gurus

I am new to mysql. I need to select the lowest number from seven
fields where the value is greater than 0. Can someone tell me how to
do this.


MySQL includes a function LEAST() which takes a variable number of
arguments and gives back the least value.

e.g. LEAST(1, 2, 99, 5) returns 1.

See http://dev.mysql.com/doc/mysql/en/co...operators.html
This will give me one long list with all the rate from (RF#) and
Rate UNtil (RU#). Now how do I select the lowest one for each
itemname?


Do you want the lowest RF# or the lowest RU# or some other computation
of them? Your other solution you posted makes me thing that you want
the lowest RF#.

Then you have to do some tricks to make sure if one of the values is 0
it won't become the least result. One method might be to force 0 (or
NULL) values to some artificial value much higher than any legitimate
value for any RF field.

SELECT LEAST(
IF(RF1,RF1,9999),
IF(RF2,RF2,9999),
IF(RF3,RF3,9999),
IF(RF4,RF4,9999),
IF(RF5,RF5,9999),
IF(RF6,RF6,9999),
IF(RF7,RF7,9999)), ItemName
FROM `Table`

I'm also inferring from your example that ItemName is a unique field,
i.e., there is only one row in Table per ItemName.

PS: The "seven fields" thing makes me wonder if you're going to have
more than seven fields in the future. Should this be better
implemented as a dependent table? It'd probably be easier to find
the MIN() for each ItemName that way too.

Regards,
Bill K.


Hi Bill

I love your solution awesome. All your inferences are correct and yes, I
should have put it in a seperate table. but I had good reasons not to.

I think I need to learn to search the Mysql site more effectively. I know
SQL from MS Access, but it is often hard to find equivalents in MySql

Thank you once more for your help

- Nicolaas

Jul 23 '05 #5
Perhaps a slight tweak on Bill's excellent solution...
SELECT LEAST(
IF(RF1,RF1,9999),
IF(RF2,RF2,9999),
IF(RF3,RF3,9999),
IF(RF4,RF4,9999),
IF(RF5,RF5,9999),
IF(RF6,RF6,9999),
IF(RF7,RF7,9999)), ItemName
FROM `Table`
The LEAST() function will ignore nulls; eg, LEAST(5, 2, NULL, 1) still
returns 1. Therefore, it's safer to do:
SELECT LEAST(
IF(RF1,RF1,NULL),
IF(RF2,RF2,NULL),
IF(RF3,RF3,NULL),
IF(RF4,RF4,NULL),
IF(RF5,RF5,NULL),
IF(RF6,RF6,NULL),
IF(RF7,RF7,NULL)), ItemName
FROM `Table`

rather than relying on the upper-bound 9999, as those upper bounds have
a pesky way of getting exceeded...

Good luck!
Michael

Jul 23 '05 #6

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

Similar topics

4
by: Lobang Trader | last post by:
Hi all, I am trying to create a username and a password class. I would like to know what are the RECOMMENDED minimum and maximum length for both fields? These fields will be something like...
5
by: Lukelrc | last post by:
Hi, I have a dynamically created listbox. I'm trying to get one of the options selected according to a passed value. This is what i have: <select name="txtTheme" id="txtTheme"> ...
2
by: John | last post by:
Hello, I have a table called BUILDREQUESTS which I want to select from, depending on the project ID of each record. The Project ID field is indexed. (A) This query runs almost instantly: ...
0
by: Andreas Håkansson | last post by:
I've been using the Muenchian method to group XML data, however I have run into a situation where I am unsure how to solve it. What I need to do is select distinct groups of items. I have an XML...
1
by: Ramesh | last post by:
hi, I am selecting fields from three table for manupulating data and i want to display total number of records selected. But i am always getting -1 value, eventhough 1000 of records are selected....
2
Chittaranjan
by: Chittaranjan | last post by:
Hi All, I have a problem and hope I can get the better solution from here. I have a form written in HTML and I need to write that in perl so the main problem I am facing is that I need to...
13
by: lane straatman | last post by:
I'm trying to figure out what data type is appropriate to represent a card in a game. The idea that I thought was going to work was a struct, foo, with two integer fields and two fields of char...
2
by: jw01 | last post by:
I have a form in which there is one combo box and three text boxes: Combo Box: -> Item A -> Item B -> Item C TextBox1: TextBox2: ...
2
by: James Harris | last post by:
I'm trying to make sense of the standard C data sizes for floating point numbers. I guess the standards were written to accommodate some particular floating point engines that were popular at one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.