Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old September 7th, 2007, 04:20 PM
Winelover Winelover is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 2 Winelover User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 27 sec
Reputation Power: 0
Trouble selecting the most recent value

Have a table like this (sorry for the formatting:
ID1 ID2 DATE ID2Value
1 12 1/1/2006 6
1 24 1/1/2006 400
1 24 6/1/2006 410
1 36 6/1/2006 100
2 12 1/1/2007 7
2 24 6/1/2007 350
2 24 7/1/2007 360
I need to return 1 row for each ID1 value - and only the ID2 value of 24 and only the most recently dated record for the multiple ID2 values - query would return:
1 24 6/1/2006 410
2 24 7/1/2007 360

I have worked and worked on this and I am still stumped (part of the problem may be I am also trying to make this work in Crystal Reports but that is for another day). I need to make this work in Oracle first. Any direction please? All suggestions appreaciated.

Many thanks,
Winelover

Reply With Quote
  #2  
Old September 7th, 2007, 07:56 PM
clivew clivew is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: Carlsbad, CA
Posts: 681 clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level)clivew User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 14 h 23 m 2 sec
Reputation Power: 12
Quote:
Originally Posted by Winelover
Have a table like this (sorry for the formatting:
ID1 ID2 DATE ID2Value
1 12 1/1/2006 6
1 24 1/1/2006 400
1 24 6/1/2006 410
1 36 6/1/2006 100
2 12 1/1/2007 7
2 24 6/1/2007 350
2 24 7/1/2007 360
I need to return 1 row for each ID1 value - and only the ID2 value of 24 and only the most recently dated record for the multiple ID2 values - query would return:
1 24 6/1/2006 410
2 24 7/1/2007 360

I have worked and worked on this and I am st ill stumped (part of the problem may be I am also trying to make this work in Crystal Reports but that is for another day). I need to make this work in Oracle first. Any direction please? All suggestions appreaciated.

Many thanks,
Winelover


Try something like:
SQL Code:
Original - SQL Code
  1.  
  2. SELECT A.ID1, A.ID2, A.DATE, A.ID2Value
  3. FROM MYTABLE A
  4. WHERE (A.ID2 = 24)
  5. AND A.DATE = (
  6.       SELECT MAX(B.DATE)
  7.       FROM MYTABLE B
  8.       WHERE (B.ID2 = 24)
  9.       AND (B.ID1 = A.ID1) )
  10.  


Clive.

Reply With Quote
  #3  
Old September 14th, 2007, 02:47 PM
Winelover Winelover is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 2 Winelover User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 27 sec
Reputation Power: 0
Thanks - that helped my thought process. In that form, took too long with my gigantic tables, but helped get me where I needed to go.

Appreciate the help.

Reply With Quote
  #4  
Old April 16th, 2008, 09:33 AM
DrewSmith70 DrewSmith70 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 1 DrewSmith70 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 58 sec
Reputation Power: 0
This is faster:

SQL Code:
Original - SQL Code
  1. SELECT * FROM(
  2.      SELECT A.ID1, A.ID2, A.DATE, A.ID2Value, MAX(A.DATE) over (partition BY A.ID2, A.ID1) max_bdate
  3.         FROM MYTABLE A
  4.        WHERE A.ID2 = 24)
  5.   WHERE DATE = max_bdate
  6.  

Last edited by DrewSmith70 : April 16th, 2008 at 09:35 AM. Reason: Added message

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Trouble selecting the most recent value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway