Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > Oracle
|
Oracle General Oracle database discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Oracle section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 26th, 2003, 03:15 PM
Registered User
 
Join Date: Oct 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Calculate de max value of two columns

How can I calculate the max value of two columns with a single query.
Suppose I have a table with the following information:

id col1 col2
-- ---- ----
1 10 20
2 20 30
3 12 23

The query should return the following data:

id max_value
-- ----------
1 20
2 30
3 23

Thanks in advance,
Carlos Dias
 
Old November 26th, 2003, 03:23 PM
Authorized User
 
Join Date: Jul 2003
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Carlos,

How about the following ?

--------------------------------------------
SQL> select * from t;

        ID COL1 COL2
---------- ---------- ----------
         1 10 20
         2 20 30
         3 12 23

SQL> select id, max(col) from (
  2 select id, col1 as col from t
  3 union
  4 select id, col2 as col from t
  5 )
  6 group by id;

        ID MAX(COL)
---------- ----------
         1 20
         2 30
         3 23

SQL>
--------------------------------------------

Cheers,
Prat







Similar Threads
Thread Thread Starter Forum Replies Last Post
Lookup max with criteria in multiple columns cc16 Excel VBA 2 September 6th, 2006 05:48 AM
Conectar textbox a campo de base de datos Access aya_hbkenshin04 ASP.NET Espanol 0 May 4th, 2006 01:26 PM
why de sql compiler do not parse de following? deniscuba SQL Language 2 February 4th, 2005 04:04 PM
How to Calculate dates in datagrid columns chiefouko VS.NET 2002/2003 2 July 15th, 2004 01:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.