Solved

SQL to conditionally delete similiar records

Posted on 2014-02-07
2
300 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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

615 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