Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oracle insert ignore duplicates

Posted on 2013-01-14
4
Medium Priority
?
872 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 38

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 600 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 1400 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

Independent Software Vendors: 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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
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.

610 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