Solved

Oracle insert ignore duplicates

Posted on 2013-01-14
4
835 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 37

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

813 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

8 Experts available now in Live!

Get 1:1 Help Now