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

x
?
Solved

Delete SQL Records Not Wanted

Posted on 2014-01-16
12
Medium Priority
?
312 Views
Last Modified: 2014-01-17
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
Comment
Question by:thayduck
[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
  • 6
  • 4
  • 2
12 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
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

by:Kevin Cross
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
;

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
 

Author Comment

by:thayduck
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Accepted Solution

by:
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
;

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
 
LVL 32

Expert Comment

by:awking00
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

by:thayduck
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

by:thayduck
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

by:thayduck
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

by:awking00
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

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

Of course,  they are 2 different cust#.
0
 

Author Closing Comment

by:thayduck
ID: 39787410
Thanks, does just what I need it to do.
0
 
LVL 60

Expert Comment

by:Kevin Cross
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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

730 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