473,425 Members | 1,771 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,425 software developers and data experts.

how to get maximum value of multiple columns with where clause?

120 100+
i can a max value of a single column with where clause like this
Expand|Select|Wrap|Line Numbers
  1. select MAX(Price_Band_1) AS HighestPrice from UK_Specials where ID IN (1,3,5,7,9,10,13,15,17,19,21,23,25,27,29,31,33,35,37)
  2.  
this works fine but I need to obtain max value of multiple columns. would it be something like this???
Expand|Select|Wrap|Line Numbers
  1. select MAX(Price_Band_1),  MAX(Price_Band_2), MAX(Price_Band_3), MAX(Price_Band_4) AS HighestPrice from UK_Specials where ID IN (1,3,5,7,9,10,13,15,17,19,21,23,25,27,29,31,33,35,37)
  2.  
many thanks in advance
Omar.
Mar 24 '10 #1

✓ answered by nbiswas

Let us start with an example.

Say I have a table(e.g. UK_Specials) something like the following

Expand|Select|Wrap|Line Numbers
  1. Id     Price_Band_1      Price_Band_2       Price_Band_3        Price_Band_4
  2. ---   ----------------------     ---------------------       --------------------       ----------------------
  3.  
  4. 1        10                        20                           30                     40
  5.  
  6. 2        35                       12                            29                     33
  7.  
  8. 3        11                       298                         12                      78
  9.  
  10. 4        56                        11                         34                      55

Since you are interested to find the maximum value of the four columns for some specified Id's , so I can say that for the above described scenario, the value will be 298 for the Id's 1,2,3,4 among all the four column.

If this understanding of mine is correct, then use any of the following

Query 1:

Expand|Select|Wrap|Line Numbers
  1. Select HighestPrice = MAX(HighestPrice) from (
  2. Select Price_Band_1 As HighestPrice from UK_Specials where Id in (1,2,3,4) UNION ALL
  3. Select Price_Band_2 from UK_Specials where Id in (1,2,3,4) 
  4. UNION ALL
  5. Select Price_Band_3 from UK_Specials where Id in (1,2,3,4) 
  6. UNION ALL
  7. Select Price_Band_4 from UK_Specials where Id in (1,2,3,4) )x
Query 2:

Expand|Select|Wrap|Line Numbers
  1. Select HighestPrice = MAX(HighestPrice) from (
  2. Select MAX(Price_Band_1) As HighestPrice from UK_Specials where Id in (1,2,3,4) UNION ALL
  3. Select MAX(Price_Band_2) from UK_Specials where Id in (1,2,3,4) 
  4. UNION ALL
  5. Select MAX(Price_Band_3) from UK_Specials where Id in (1,2,3,4) 
  6. UNION ALL
  7. Select MAX(Price_Band_4) from UK_Specials where Id in (1,2,3,4) )x
Output:

Expand|Select|Wrap|Line Numbers
  1. HighestPrice
  2. 298
Note: ~
If you are using Sql Server 2005+, then instead of doing the above, first you UNPIVOT
and then apply the MAX function to achieve the same.

Hope this helps
Let me know in case of any concern.

8 11153
ck9663
2,878 Expert 2GB
Well did you try the it?

Happy Coding!!!

~~ CK
Mar 24 '10 #2
omar999
120 100+
hey ck9663

sorry what do you mean?
Mar 24 '10 #3
ck9663
2,878 Expert 2GB
I mean did you try this code?

Expand|Select|Wrap|Line Numbers
  1.    select MAX(Price_Band_1),  MAX(Price_Band_2), MAX(Price_Band_3), MAX(Price_Band_4) AS HighestPrice from UK_Specials where ID IN (1,3,5,7,9,10,13,15,17,19,21,23,25,27,29,31,33,35,37)
  2.  
  3.  
Mar 24 '10 #4
omar999
120 100+
i tried the code and it returns an incorrect value without any errors on the page.
I believe there is some sort of mistake in my select statement logic but i cant see where
Mar 24 '10 #5
nbiswas
149 100+
Let us start with an example.

Say I have a table(e.g. UK_Specials) something like the following

Expand|Select|Wrap|Line Numbers
  1. Id     Price_Band_1      Price_Band_2       Price_Band_3        Price_Band_4
  2. ---   ----------------------     ---------------------       --------------------       ----------------------
  3.  
  4. 1        10                        20                           30                     40
  5.  
  6. 2        35                       12                            29                     33
  7.  
  8. 3        11                       298                         12                      78
  9.  
  10. 4        56                        11                         34                      55

Since you are interested to find the maximum value of the four columns for some specified Id's , so I can say that for the above described scenario, the value will be 298 for the Id's 1,2,3,4 among all the four column.

If this understanding of mine is correct, then use any of the following

Query 1:

Expand|Select|Wrap|Line Numbers
  1. Select HighestPrice = MAX(HighestPrice) from (
  2. Select Price_Band_1 As HighestPrice from UK_Specials where Id in (1,2,3,4) UNION ALL
  3. Select Price_Band_2 from UK_Specials where Id in (1,2,3,4) 
  4. UNION ALL
  5. Select Price_Band_3 from UK_Specials where Id in (1,2,3,4) 
  6. UNION ALL
  7. Select Price_Band_4 from UK_Specials where Id in (1,2,3,4) )x
Query 2:

Expand|Select|Wrap|Line Numbers
  1. Select HighestPrice = MAX(HighestPrice) from (
  2. Select MAX(Price_Band_1) As HighestPrice from UK_Specials where Id in (1,2,3,4) UNION ALL
  3. Select MAX(Price_Band_2) from UK_Specials where Id in (1,2,3,4) 
  4. UNION ALL
  5. Select MAX(Price_Band_3) from UK_Specials where Id in (1,2,3,4) 
  6. UNION ALL
  7. Select MAX(Price_Band_4) from UK_Specials where Id in (1,2,3,4) )x
Output:

Expand|Select|Wrap|Line Numbers
  1. HighestPrice
  2. 298
Note: ~
If you are using Sql Server 2005+, then instead of doing the above, first you UNPIVOT
and then apply the MAX function to achieve the same.

Hope this helps
Let me know in case of any concern.
Mar 25 '10 #6
omar999
120 100+
this has worked beautifully! thank you ever so much and have a wonder ful day

kind regards
Omar : )
Mar 25 '10 #7
omar999
120 100+
hi - im now using this method to return the minimum value from selected columns. however for some non obvious reason it only works when a 3digit long number is entered? below is my sql statement;
Expand|Select|Wrap|Line Numbers
  1. Select CheapestPrice = MIN(CheapestPrice) from (Select MIN(Price_Band_1) As CheapestPrice from mytablename 
  2. where Id in (1,3) UNION ALL Select MIN(Price_Band_2) from mytablename 
  3. where Id in (1,3) UNION ALL Select MIN(Price_Band_3) from mytablename 
  4. where Id in (1,3) UNION ALL Select MIN(Price_Band_4) from mytablename where Id in (1,3) )x
my table with price_band columns, values in bold;
Expand|Select|Wrap|Line Numbers
  1. ID | Flight_Route | Price_Band_1 | Month_Band_1 | Date_Band_1 | Price_Band_2 | Month_Band_2 | Date_Band_2 | Price_Band_3 | Month_Band_3 | Date_Band_4 | Price_Band_4 | Month_Band_4 | Date_Band_4
  2. 1    TEST1    99    MAY    12, 19, 31    199    JUNE    12, 19, 31    199    JULY    12, 19, 31    199    AUG    12, 19, 31
  3. 3    TEST3    199    MAY    12, 19, 31    199    JUNE    12, 19, 31    199    JULY    12, 19, 31    199    AUG    12, 19, 31
  4.  
the above code returns 199 when it really should return 99... any ideas as to why?

please advise
Omar.
Apr 3 '10 #8
The same problem happens if you use MAX function.
In that case, if you try to use numbers like 123, 234 and 5 the result will be 5 that means that the function look only at the first digit of the number.

I've solved this issue by force the values as INT.
So in your case it could be something like the following:

DECLARE @value1 int, @value3 int, @value2 int

SELECT @value1 = Select Price_Band_1 from UK_Specials where Id in (1,2,3,4)
SELECT @value2 = Select Price_Band_2 from UK_Specials where Id in (1,2,3,4)
SELECT @value3 = Select Price_Band_3 from UK_Specials where Id in (1,2,3,4)

Select HighestPrice = MAX(HighestPrice) from (
SELECT HighestPrice = @value1
UNION ALL
SELECT HighestPrice = @value2
UNION ALL
SELECT HighestPrice = @value3
)x

Sorry for my poor english, hope this helps!
Matteo.
Apr 11 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: steve | last post by:
To gain performance, do I need to index ALL the fields in the where clause. Say we have a query like: select stuff from table where field1=.. and field2=... If field1 selection substantially...
17
by: Darek | last post by:
Hi, I have a table, something similar to: create table my_table ( id char(32) not null primary key, num integer not null, code varchar(2) not null, name varchar(60) not null,
2
by: pb648174 | last post by:
I frequently have the problem where I have a list of items to delete in a temp table, such as ProjectId Description ------------- ---------------- 1 ...
5
by: John Devlon | last post by:
Hi, Does anyone know how to get a value of a second column of a selected item in Listview. I've create a listview and added this code Listview.Items.Clear() Listview.Columns.Clear()...
0
by: jobs | last post by:
I have a gridview that times when selecting from specific table when I try to when I add a where clause. Even when I'm only seleting Top 1 which comes right back at the command line. I test the...
2
by: Jim.Mueksch | last post by:
I am having a problem with using calculated values in a WHERE clause. My query is below. DB2 gives me this error message: Error: SQL0206N "APPRAISAL_LESS_PRICE" is not valid in the context where...
2
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I need to locate a row (or see if it exists) in a table contained in a dataset. The DataView.Find method seems to work OK for one criteria but I need to use 2 criterias. Is there...
2
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
Is there a resource limit on how many items can be put in the Where clause of a select command that retrieves rows to a OleDbDataAdapter dataset? Can it be unlimited if I change some settings? ...
4
by: Bernard Dhooghe | last post by:
Table definition: CREATE TABLE "SCHEMA1 "."X2" ( "C1" CHAR(20) NOT NULL , "C2" CHAR(10) NOT NULL , "C3" CHAR(30) NOT NULL GENERATED ALWAYS AS (C1|| C2) ) IN "USERSPACE1" ; -- DDL...
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:
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...
1
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.