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 > Data Access, Manipulation & Batch Languages > ANSI SQL > row with highest column A/B/C/D (was "SQL: is this possible?")

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-20-06, 05:06
irenicuz irenicuz is offline
Registered User
 
Join Date: Feb 2004
Posts: 34
row with highest column A/B/C/D (was "SQL: is this possible?")

Hi i am wondering if this is possible with SQL:

Record Nr/Field A/Field B/Field C/Field D
1/100/200/500/1000
2/50/300/250/500
3/50/100/1000/500

Is there any possibility with SQL to select all data from this table where the following criteria are used:

Field A highest ? Yes: take this record
No: Field B highest ? Yes: take this record
No: Field C highest ? Yes: take this record
No: Field D highest ? Yes: take this record

This will result in record 1 to be taken (Field A is highest)

Other data:

Record Nr/Field A/Field B/Field C/Field D
1/100/200/500/1000
2/100/200/750/500
3/50/100/1000/500

This will result in record 2 to be taken (Field C is higest)

Any help is appreciated, Thanks
Reply With Quote
  #2 (permalink)  
Old 07-20-06, 05:14
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
If I understand you correctly, this SQL will return all the rows with the one you want at the top:
Code:
select * from mytable order by a desc,b desc,c desc,d desc;
All you then need to do is take just the first record from that result set. How you do that varies from one DBMS to another.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 07-20-06, 05:15
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if a row (not a record) has 4 columns (not fields), then one of them is going to have the highest value in the row, and so you want that row (not record) to be returned?

sounds like you want all rows returned, because each row will have a highest column value amongst its 4 column values

or do you want values compared amongst all rows at the same time? in that case column C in row 2 does not have the highest value

confused
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 07-20-06, 05:18
irenicuz irenicuz is offline
Registered User
 
Join Date: Feb 2004
Posts: 34
Quote:
Originally Posted by andrewst
If I understand you correctly, this SQL will return all the rows with the one you want at the top:
Code:
select * from mytable order by a desc,b desc,c desc,d desc;
All you then need to do is take just the first record from that result set. How you do that varies from one DBMS to another.
I just started to experiment with this This MUST be the solution i was looking for. Thanks

Edit: I had to select the LAST record to get the correct record (MS Access). Strange, but it works.

Last edited by irenicuz; 07-20-06 at 05:34.
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