Solved

Oracle insert ignore duplicates

Posted on 2013-01-14
4
823 Views
Last Modified: 2013-01-15
I have a table with multiple rows.  I want to have another table (identical structure) with just one row per key value (i.e. the row with the latest date).

Input data example

Red, 1/1/1906,  Color is red
Red, 7/31/2006,  Color is red

I only want one record per color.  F
or Red this would be the 7/31/2006 record.

I thought if I ran code like below that I could sort the data so the highest date would be first.  then if I loaded the results of that SQL into the new table with a unique constrainst then it would load the one with the highest date, ignoring subsequent values for color.


INSERT INTO COLORS_SINGLE (COLOR, EFFDT, DESCR)
SELECT COLOR, EFFDT, DESCR
FROM COLORS_ALL
WHERE EFF_STATUS = 'A'
ORDER BY COLOR, EFFDT desc, DESCR;


This does not work.  it fails

Any suggestions?  thanks
0
Comment
Question by:stirus
4 Comments
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 38776241
add a group by

INSERT INTO COLORS_SINGLE (COLOR, EFFDT, DESCR)
SELECT COLOR, MAX(EFFDT), DESCR
FROM COLORS_ALL
WHERE EFF_STATUS = 'A'
GROUP BY COLOR, DESCR
ORDER BY COLOR, EFFDT desc, DESCR;
0
 
LVL 8

Assisted Solution

by:virtuadept
virtuadept earned 150 total points
ID: 38776255
Your where clause needs to get the maximimum effective date and only pull those records.

INSERT INTO COLORS_SINGLE (COLOR, EFFDT, DESCR)
SELECT COLOR, EFFDT, DESCR
FROM COLORS_ALL
WHERE EFF_STATUS = 'A'
AND EFFDT = (SELECT MAX(EFFDT) FROM COLORS_ALL as B WHERE B.COLOR = COLORS_ALL.COLOR AND B.EFF_STATUS = 'A' )
ORDER BY COLOR, EFFDT desc, DESCR;

Open in new window

0
 
LVL 73

Accepted Solution

by:
sdstuber earned 350 total points
ID: 38776306
INSERT INTO COLORS_SINGLE (COLOR, EFFDT, DESCR)
select color,effdt,descr from
(SELECT COLOR, EFFDT, DESCR, row_number() over(partition by color order by effdt desc) rn
FROM COLORS_ALL
WHERE EFF_STATUS = 'A'
)
where rn = 1



using analytics vs aggregates allows you to partition (group) by only the color column, not the color and description.

also, they should be more efficient than querying the table twice, even if there is an index on date
0
 

Author Closing Comment

by:stirus
ID: 38779223
virtuadept was good solution that solved my problem and I understood it.

sdstuber is excellent solution that also taught me something new.

so I increased points to 500 to recognize both of you.  THANIKS!
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.

Join & Write a Comment

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

757 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

20 Experts available now in Live!

Get 1:1 Help Now