?
Solved

Oracle insert ignore duplicates

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many‚Ķ
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 shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
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.
Suggested Courses

764 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