If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Returning multiple MIN values of a field based upon different criteria

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-13-10, 18:10
JebusChristmas JebusChristmas is offline
Registered User
 
Join Date: Jan 2010
Location: Orange County, CA
Posts: 5
Returning multiple MIN values of a field based upon different criteria

The idea is to find the Min `Inventory` value for each `Tank` for a specified `Site Number` not the single lowest inventory value for a specific `Site Number`

Code:
SELECT DISINCT `Tank`
FROM `detail`
WHERE `Site Number` = 9201906000;
Returns:

|Tank
|-----
|1
|2
|3
|-----

Code:
SELECT `Tank`, `Grade`, Min(`Inventory`) as 'Min Inventory'
FROM `detail`
WHERE `Site Number`= 9201906000 and `Tank` = *Variable Criteria*;
Desired Result: is attached as image

The Field named `Tank` contains a simple 1, 2, 3, or 4 value that identifies the tank.

All fields are located in the same table. I can do this w/ VBScript, but I'm sure there has to be a way using only SQL.
Attached Thumbnails
Returning multiple MIN values of a field based upon different criteria-result.gif  
Reply With Quote
  #2 (permalink)  
Old 04-13-10, 18:28
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi Chad,

try using GROUP BY. This should resolve your problem.

SELECT `Tank`, `Grade`, Min(`Inventory`) as 'Min Inventory'
FROM `detail`
WHERE `Site Number`= 9201906000 and `Tank` = *Variable Criteria*
GROUP BY `Tank`, `Grade`;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 04-13-10, 18:32
JebusChristmas JebusChristmas is offline
Registered User
 
Join Date: Jan 2010
Location: Orange County, CA
Posts: 5
Simple Enough

Well Jebus Christmas, my brain was farting. Thank you for wafting away the fumes.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On