Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL to conditionally delete similiar records

Posted on 2014-02-07
2
Medium Priority
?
301 Views
Last Modified: 2014-02-07
I've got a really tricky SQL script to write to clean up some data.   I'll try to explain it. Can anyone help with this:

I have a table called Products, that I've simplified below.

ID     Product                     Manu              Color
1       xyz123_01                  Acme              Green
2       xzy123_01_1               Acme             Green
3       xyz123_01_2               Acme              Green
4       abc100_05                 Jones Co.          Red
5       abc100_05_1                Jones Co.           Pink

I need to create a script that will delete records #2 and #3 here, because they share Manu, Color and the first 9 characters of Product field as #1.  Record #1 should remain though.

Products #4 and #5 would both remain, because they have differing Color fields.

The resulting table, called Products2 should look like this:

ID     Product                     Manu              Color
1       xyz123_01                  Acme              Green
4       abc100_05                 Jones Co.          Red
5       abc100_05_1                Jones Co.           Pink
0
Comment
Question by:Xbradders
[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
2 Comments
 
LVL 17

Accepted Solution

by:
Kent Dyer earned 1600 total points
ID: 39842585
SELECT DISTINCT LEFT(PRODUCT, 9), MENU, COLOR FROM SOME_TABLE

Then you could do something like..

SELECT * FROM SOME_TABLE
WHERE NOT IN (SELECT DISTINCT LEFT(PRODUCT, 9), MENU, COLOR FROM SOME_TABLE)

Once you have the SELECT nailed down, then setup your delete.

Hope This helps.
0
 

Author Comment

by:Xbradders
ID: 39842788
Great, thanks Kent.  I've built my select, but can't seem to get the WHERE NOT IN clause to get the tracks to delete.  Forgive me, my sql is rusty and I'm in MySQL now which may not support this...  This is what I've tried:

SELECT * FROM test.sm_tracks
WHERE NOT IN
(SELECT distinct LEFT(CatNo,9),
Composer1, COMPOSER1_SHARE,
Composer2, COMPOSER2_SHARE,
Composer3, COMPOSER3_SHARE,
Composer4, COMPOSER4_SHARE,
Composer5, COMPOSER5_SHARE,
Composer6, COMPOSER6_SHARE,
PUBLISHER1, PUBLISHER1_SHARE,
PUBLISHER2, PUBLISHER2_SHARE,
PUBLISHER3, PUBLISHER3_SHARE,
PUBLISHER4, PUBLISHER4_SHARE,
PUBLISHER5, PUBLISHER5_SHARE,
PUBLISHER6, PUBLISHER6_SHARE,
ARRANGER1, ARRANGER1_SHARE,
ARRANGER2, ARRANGER2_SHARE,
ARRANGER3, ARRANGER3_SHARE,
ARRANGER4, ARRANGER4_SHARE,
ARRANGER5, ARRANGER5_SHARE,
ARRANGER6, ARRANGER6_SHARE
FROM test.sm_tracks);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 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