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 Rating: Thread Rating: 2 votes, 3.00 average. Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 5th, 2003, 02:01 PM
scottcka scottcka is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 scottcka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question SELECT DISTINCT on multiple fields

Hi,

I think I'm having difficulty trying to extract records using SELECT DISTINCT. I'm doing SELECT DISTINCT FLD1, FLD2, FLD3 and I get the following,

FLD1 FLD2 FLD3
---------------------------
Dept1 ABC 04-FEB-03
Dept1 ABC 15-JUN-03
Dept1 ABC 27-AUG-03

Whic is not the result I want. I only need to get the last one with the lastest date which is

Dept1 ABC 27-AUG-03

How can I get the distinct record with the lastest date field ( FLD3 ) when there are multiple records on one field (FLD1) but different date value on other field ( FLD3 )?

TIA for your help,
Scottcka

Reply With Quote
  #2  
Old September 6th, 2003, 01:45 PM
Pankaj77 Pankaj77 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 Pankaj77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post

Hi Scottika ,

u can get ur result this way ....

select distinct T.FLD1,T.FLD2, T.FLD3
from TABLE1 T
where T.FLD3 = (select max(FLD3)
from TABLE1
where FLD1 = T.FLD1)

try it out.

Reply With Quote
  #3  
Old September 6th, 2003, 03:33 PM
hedge hedge is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2002
Posts: 693 hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level)hedge User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 38 m 9 sec
Reputation Power: 20
sql Code:
Original - sql Code
  1. SELECT
  2. fld1, fld2, MAX(fld3)
  3.   FROM table1
  4. GROUP BY fld1, fld2

Last edited by pabloj : August 1st, 2006 at 05:59 AM. Reason: Please no php code to highlight SQL ;)

Reply With Quote
  #4  
Old November 24th, 2003, 09:06 PM
rizzo rizzo is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 7 rizzo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SELECT DISTINCT Field1, Field2 MAX(Field3)
FROM Table
GROUP BY Field1

Reply With Quote
  #5  
Old August 1st, 2006, 05:32 AM
puvi puvi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 2 puvi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 41 m 29 sec
Reputation Power: 0
Thumbs up Re:problem

Hi,

if the data is like this,

ID NAME DAT
--- ------------------------------ ---------
1 aa 12-JAN-06
1 aa 13-JAN-05
2 mm 14-JAN-05
2 mm 04-MAR-06

then use this query to get ur result
" select id,name,dat from test1 where dat in (select max(dat) from test1 group by id,name);"

The result will be :
1 aa 12-jan-06
2 mm 04-mar-06

or if u have data like this

ID NAME DAT
--- ------------------------------ ---------
1 aa 12-JAN-06
1 aa 13-JAN-05
1 aa 14-JAN-05
1 aa 04-MAR-06

" select id,name,dat from test1 where dat = (select max(dat) from test1 group by id,name);"

the result will be:

1 aa 04-mar-06

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > SELECT DISTINCT on multiple fields


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 6 hosted by Hostway
Stay green...Green IT