Solved

Oracle insert ignore duplicates

Posted on 2013-01-14
4
857 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 74

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
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.
Via a live example, show how to take different types of Oracle backups using RMAN.

707 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