Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Delete SQL Records Not Wanted

Posted on 2014-01-16
Medium Priority
315 Views
Table1

Custno          Inactive     Inactivedt
P123456           N            10/12/2013
P123456           N            11/22/2013
P123456           Y             02/01/2013
P123456           Y             03/01/2013
P456123           N            01/15/2014
P456123           Y             12/10/2013
P456123           Y             11/04/2013
P789456           Y             01/10/2014
P789456           Y             09/15/2013

How would I delete the same customer numbers that Inactive = Y and only keep 1 of the same customer numbers that is = N  with the latest Inactive date

Or,  if all same customers only have a Inactive = Y, then only keep that customer with the latest date and delete the other one.

Table1  (should look like when done )

Custno          Inactive     Inactivedt
P123456           N            11/22/2013
P456123           N            01/15/2014
P789456           Y             01/10/2014

So, only keep the latest Inactive = N record with the same customer numbers and delete the rest

or,  if no Inactive = N then only keep the latest Inactive = Y customer number
0
Question by:thayduck
• 6
• 4
• 2

LVL 60

Expert Comment

ID: 39786222
Is it more simply the record with the latest Inactivedt?
Either way, one answer using SQL 2008 is to create a CTE that adds a row number column.  The answer to the above will determine the ORDER BY; otherwise, the solution is the same as the final query will be a DELETE that filters out records with RN > 1.
0

LVL 60

Expert Comment

ID: 39786241
Here is a self-contained working example of what I mean:
``````DECLARE @your_table TABLE(
Custno VARCHAR(10),
Inactive CHAR(1),
Inactivedt DATE
);

INSERT INTO @your_table(Custno, Inactive, Inactivedt)
VALUES('P123456','N','10/12/2013'),
('P123456','N','11/22/2013'),
('P123456','Y','02/01/2013'),
('P123456','Y','03/01/2013'),
('P456123','N','01/15/2014'),
('P456123','Y','12/10/2013'),
('P456123','Y','11/04/2013'),
('P789456','Y','01/10/2014'),
('P789456','Y','09/15/2013')
;

;WITH cte AS (
/* Rank records based on Inactivedt descending. */
SELECT Custno, Inactive, Inactivedt
, RN = ROW_NUMBER()
OVER(PARTITION BY Custno
ORDER BY Inactivedt DESC)
FROM @your_table
)
/* Delete old records. */
DELETE FROM cte WHERE RN > 1;

SELECT Custno, Inactive, Inactivedt
FROM @your_table
;
``````

Results of SELECT after DELETE:
``````Custno     Inactive Inactivedt
---------- -------- ----------
P123456    N        2013-11-22
P456123    N        2014-01-15
P789456    Y        2014-01-10
``````

I hope that helps!

Best regards and happy coding,

Kevin
0

Author Comment

ID: 39786243
The same customer# could have the same Inactive date where 1 record is a N and the other one is a Y.
0

LVL 60

Accepted Solution

Kevin Cross earned 700 total points
ID: 39786268
See if this meets your needs:
``````DECLARE @your_table TABLE(
Custno VARCHAR(10),
Inactive CHAR(1),
Inactivedt DATE
);

INSERT INTO @your_table(Custno, Inactive, Inactivedt)
VALUES('P123456','N','10/12/2013'),
('P123456','Y','11/22/2013'), -- same inactivedt as 'N'
('P123456','N','11/22/2013'),
('P123456','Y','02/01/2013'),
('P123456','Y','03/01/2013'),
('P456123','N','01/15/2014'),
('P456123','Y','12/10/2013'),
('P456123','Y','11/04/2013'),
('P789456','Y','01/10/2014'),
('P789456','Y','09/15/2013')
;

;WITH cte AS (
/* Rank records based on Inactivedt descending. */
SELECT Custno, Inactive, Inactivedt
, RN = ROW_NUMBER()
OVER(PARTITION BY Custno
ORDER BY Inactivedt DESC, Inactive)
FROM @your_table
)
/* Delete old records. */
DELETE FROM cte WHERE RN > 1;

SELECT Custno, Inactive, Inactivedt
FROM @your_table
;
``````

It uses a secondary sort on the [Inactive] column, so 'N' ranks before 'Y'.  If the real values in the table do not sort naturally, you can use the following.
``````CASE [Inactive] WHEN 'first choice value' THEN 1 ELSE 2 END
``````

You would insert that in the ORDER BY of the OVER() clause in place of "Inactive" above.
0

LVL 32

Expert Comment

ID: 39786619
Is it possible for a custno with an inactive value of 'Y' to have an inactivedt greater than any inactivedt where the inactive value is 'N' for that custno? For example, could this condition exist?
P123456           Y            12/20/2013
P123456           N            10/12/2013
P123456           N            11/22/2013
P123456           Y             02/01/2013
P123456           Y             03/01/2013
If so, what would you want the result to be?
0

Author Comment

ID: 39786688
As long as there is a N record, I want that N record with the latest date no matter what date is in the Y record.

I only want the latest Y record when there is no N record.
0

Author Comment

ID: 39786721
Kevin, your code is pulling in wrong record.  It is pulling in the 06/08/2013 record.

F149200SC      Y      06/08/2013
P149200SC      N      01/16/2014

F149200SC            Y      2013-06-08   rec it left in table.
0

Author Comment

ID: 39786727
here is code i ran:

;WITH cte AS (
/* Rank records based on Inactivedt descending. */
SELECT Cust#, Inactive, Inactivedt
, RN = ROW_NUMBER()
OVER(PARTITION BY Cust#
ORDER BY Inactivedt DESC, Inactive)
FROM #gpcustwork
)
/* Delete old records. */
DELETE FROM cte WHERE RN > 1;
0

LVL 32

Expert Comment

ID: 39786843
>>F149200SC      Y      06/08/2013
P149200SC      N      01/16/2014

F149200SC            Y      2013-06-08   rec it left in table. <<
Because it's partitioning by cust# and F149200SC is a different cust# than P149200SC. If they, in fact, represent the same customer, then you could partition by the substring of the cust# beginning with the second character.
0

Author Comment

ID: 39787400
Man, I am sorry, how did I miss that.

Of course,  they are 2 different cust#.
0

Author Closing Comment

ID: 39787410
Thanks, does just what I need it to do.
0

LVL 60

Expert Comment

ID: 39789302
I am glad that helped.  BTW, if you want any 'N' record regardless of 'Y' dates, you can swap the ORDER BY.  Therefore, it will order 'N' before 'Y', then sort by date descending.  Hence, the only way for a 'Y' record to have the rank 1 is for no 'N' records exist.

Best regards,

Kevin
0

## Featured Post

Question has a verified solution.

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

Why is this different from all of the other step by step guides? Â Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff lâ€¦
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backupâ€¦
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.
###### Suggested Courses
Course of the Month11 days, 19 hours left to enroll