Delete SQL Records Not Wanted

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
thayduckProgrammer AnalystAsked:
Who is Participating?
 
Kevin CrossConnect With a Mentor Chief Technology OfficerCommented:
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
;

Open in new window


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

Open in new window


You would insert that in the ORDER BY of the OVER() clause in place of "Inactive" above.
0
 
Kevin CrossChief Technology OfficerCommented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
;

Open in new window


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

Open in new window


I hope that helps!

Best regards and happy coding,

Kevin
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
thayduckProgrammer AnalystAuthor Commented:
The same customer# could have the same Inactive date where 1 record is a N and the other one is a Y.
0
 
awking00Commented:
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
 
thayduckProgrammer AnalystAuthor Commented:
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
 
thayduckProgrammer AnalystAuthor Commented:
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
 
thayduckProgrammer AnalystAuthor Commented:
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
 
awking00Commented:
>>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
 
thayduckProgrammer AnalystAuthor Commented:
Man, I am sorry, how did I miss that.

Of course,  they are 2 different cust#.
0
 
thayduckProgrammer AnalystAuthor Commented:
Thanks, does just what I need it to do.
0
 
Kevin CrossChief Technology OfficerCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.