Solved

Returning a rows maximum value

Posted on 2008-06-23
4
1,205 Views
Last Modified: 2013-12-19
I want to query the table to get back the maximum value (in this instance, the most recent date) from each row. Please see attached for sample data (fig 1) and desired output (fig. 2)

I could probably do this using the attached sql but was wondering if there was a more efficient way of doing this?

thanks
select

client_id,

max(date_field) max_date

from

(select Client_id, start_date as date_field from sample_table

union

select Client_id, end_date as date_field from sample_table

union

select Client_id, update_date as date_field from sample_table

union

select Client_id, reassign_date as date_field from sample_table)

group by

client_id

Open in new window

Sample.xls
0
Comment
Question by:tonMachine100
  • 2
4 Comments
 
LVL 14

Assisted Solution

by:ajexpert
ajexpert earned 50 total points
ID: 21848109
If you know that there is no duplicate data in sample_table then use UNION ALL instead of UNION as UNION ALL will definately improve performance
select

client_id,

max(date_field) max_date

from

(select Client_id, start_date as date_field from sample_table

UNION ALL

select Client_id, end_date as date_field from sample_table

UNION ALL 

select Client_id, update_date as date_field from sample_table

UNION ALL

select Client_id, reassign_date as date_field from sample_table)

group by

client_id

Open in new window

0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 21848201
Try GREATEST() function:

SQL> Alter Session Set Nls_Date_Format='Mm/Dd/Yyyy';
 

Session altered.
 

SQL> 

SQL> With Sample_Table As (

  2  Select 1234  Client_Id

  3  , '2/1/2008' Start_Date

  4  , '6/5/2008' End_Date

  5  , '6/4/2008' Update_Date

  6  , '6/3/2008' Reassign_Date

  7  From Dual Union

  8  Select 1235,'8/5/2006',Null,'9/5/2006',Null From Dual Union

  9  Select 1236,'9/7/2005','10/8/2008','10/15/2006',Null From Dual Union

 10  Select 1237,'1/4/2008','5/4/2008','2/3/2008','2/3/2008' From Dual Union

 11  Select 1238,'5/1/2008','6/6/2008',Null,Null From Dual)

 12  Select Client_Id

 13       , Greatest(Nvl(Start_Date,'1/1/1900')

 14                 ,Nvl(End_Date,'1/1/1900')

 15                 ,Nvl(Update_Date,'1/1/1900')

 16                 ,Nvl(Reassign_Date,'1/1/1900'))

 17    From Sample_Table

 18  /
 

 CLIENT_ID GREATEST(N

---------- ----------

      1234 6/5/2008

      1235 9/5/2006

      1236 9/7/2005

      1237 5/4/2008

      1238 6/6/2008
 

SQL> 

Open in new window

0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 50 total points
ID: 21848261
Ooops, her is with date format corrected:

SQL> Alter Session Set Nls_Date_Format='Mm/Dd/Yyyy';

 

Session altered.

 

SQL> With Sample_Table As (

  2  Select 1234  Client_Id

  3  , '02/01/2008' Start_Date

  4  , '06/05/2008' End_Date

  5  , '06/04/2008' Update_Date

  6  , '06/03/2008' Reassign_Date

  7  From Dual Union

  8  Select 1235,'08/05/2006',Null,'09/05/2006',Null From Dual Union

  9  Select 1236,'09/07/2005','10/08/2008','10/15/2006',Null From Dual Union

 10  Select 1237,'01/04/2008','05/04/2008','02/03/2008','02/03/2008' From Dual Union

 11  Select 1238,'05/01/2008','06/06/2008',Null,Null From Dual)

 12  Select Client_Id

 13       , Greatest(Nvl(Start_Date,'01/01/1900')

 14                 ,Nvl(End_Date,'01/01/1900')

 15                 ,Nvl(Update_Date,'01/01/1900')

 16                 ,Nvl(Reassign_Date,'01/01/1900'))

 17*   From Sample_Table

SQL> /
 

 CLIENT_ID GREATEST(N

---------- ----------

      1234 06/05/2008

      1235 09/05/2006

      1236 10/15/2006

      1237 05/04/2008

      1238 06/06/2008
 

SQL> 

Open in new window

0
 
LVL 32

Accepted Solution

by:
awking00 earned 150 total points
ID: 21849387
see attached.
maxdate.txt
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle query help 29 77
oracle report printing 2 pages in one page 2 58
scheduler for Procedure in DB with 3 arguments in 10g 7 29
sort a spool into file output in oracle 1 22
Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now