SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


delete records from table which are not active.


delete records from table which are not active.

Author
Message
mcfarlandparkway
mcfarlandparkway
SSCertifiable
SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)SSCertifiable (6.7K reputation)

Group: General Forum Members
Points: 6720 Visits: 870
I have a table, where it has status like active, terminated.

I have checked the duplicate records in the table with PNID how many active and how many terminated are there.. I found only one record.

select PNID, count(*) from ActiveStatus group by PNID HAVING count(*) > 1; -- I found only one PNID which has active and terminated record.

But when I search with PNUI

select PNUI, count(*) from ActiveStatus group by PNUI HAVING count(*) > 1; -- I found a lot
I took one id and search the data .. looks like
PNID PNUI status
68765 01844 Active
10002 01844 terminate
11189 01844 terminate
19189 01844 Active


delete from ActiveStatus
where staus in ('Terminate') and PNID in (select PNUI
from activestatus
group by PNUI) having count(*)>1

Is this query correct to delete records?


drew.allen
drew.allen
SSC Guru
SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)SSC Guru (67K reputation)

Group: General Forum Members
Points: 67206 Visits: 17736
You're asking for advice from the Internet on what records to delete from your database?

Only you can say whether the query is right, but you should ALWAYS TEST by first SELECTing records with the same criteria. I can, however, tell you that this particular query is wrong.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Joe Celko
Joe Celko
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7248 Visits: 3909
mcfarlandparkway - Wednesday, January 3, 2018 2:39 PM


>> I have a table, where it has status like active, terminated. <<

Where is the DDL for this table? Keys? Constraints? Data types? Did you read the basic forum rules before you posted?

I don’t you understand the basic principle of data modeling. Status is from the Latin word for a “state of being” And it implies that it has a temporal dimension. For example, if I had a properly named table instead of your vague generic data element name, such as “employment_status”, it’s values would depend on the time interval. If they were currently employed then the end of my interval would be a NULL to indicate that it has not completed yet.

Please read this article:

https://www.red-gate.com/simple-talk/sql/t-sql-programming/state-transition-constraints/

>>I have checked the duplicate records [sic] in the table with pn_id how many active and how many terminated are there.. I found only one record [sic].<<

Please learn the difference between a row and a record. This is a fundamental concept in RDBMS. Now it’s time to start guessing at your DDL and correcting it. Even though we can’t see it Sad

SELECT pn_id, COUNT(*)
FROM Active_Status --- Invalid table name
GROUP BY pn_id HAVING COUNT(*) > 1;

You don’t understand what a table is! The “active status” is not a set of entities or a relationship, but is a value of an attribute inside a table.

-- I found only one pn_id which has active and terminated record [sic].

>> Is this query [sic]] correct to delete records [sic]?<<

This is a statement, not a query.


Please post DDL and follow ANSI/ISO standards when asking for help.
TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)

Group: General Forum Members
Points: 123476 Visits: 8995
Go away Joe.

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
aaron.reese
aaron.reese
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11606 Visits: 1094
Joe,

Don't go away but how about a new years resolution to try and be a bit less aggressive towards newbies and those for whom English is possibly not even a second language. You have a wealth on knowledge and a passion for 'proper' database structures acquired through many decades of experience. Some members have inherited bad designs and as junior members may not have the experience, knowledge, gravitas or system credentials to 'fix' the issues as you see them.

A lot of the time the real data cannot be posted either because it is subject to NDA or data security rules like european GDPR protections or the actual domain model is so complex that to post real data would only obfuscate rather than describe the nature of the problem.
aaron.reese
aaron.reese
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11606 Visits: 1094
Joe,

Don't go away but how about a new years resolution to try and be a bit less aggressive towards newbies and those for whom English is possibly not even a second language. You have a wealth on knowledge and a passion for 'proper' database structures acquired through many decades of experience. Some members have inherited bad designs and as junior members may not have the experience, knowledge, gravitas or system credentials to 'fix' the issues as you see them.

A lot of the time the real data cannot be posted either because it is subject to NDA or data security rules like european GDPR protections or the actual domain model is so complex that to post real data would only obfuscate rather than describe the nature of the problem.

having said all of that, a member with 4K+ points should know better ...
TheSQLGuru
TheSQLGuru
SSC Guru
SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)SSC Guru (123K reputation)

Group: General Forum Members
Points: 123476 Visits: 8995
aaron.reese - Thursday, January 4, 2018 7:47 AM
Joe,

Don't go away but how about a new years resolution to try and be a bit less aggressive towards newbies and those for whom English is possibly not even a second language. You have a wealth on knowledge and a passion for 'proper' database structures acquired through many decades of experience. Some members have inherited bad designs and as junior members may not have the experience, knowledge, gravitas or system credentials to 'fix' the issues as you see them.

A lot of the time the real data cannot be posted either because it is subject to NDA or data security rules like european GDPR protections or the actual domain model is so complex that to post real data would only obfuscate rather than describe the nature of the problem.

Sorry, but I completely disagree with this post. His knowledge is archaic at best and COMPLETELY not applicable or inefficient or both to modern versions of SQL Server at worst. Right or wrong, people just do NOT develop SQL Server database applications the way his Ivory Tower world believes they should be done. And there is the fact that he has behaved the way he does for as long as I have known him (and I will bet long before that), and MANY people have asked him to change over the decades. So he either cannot or will not.

So IMO he has absolutely no replying to questions on SSC.com, or any other SQL Server-focused forum.


Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
RandomStream
RandomStream
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3045 Visits: 648

Let's focus on OP's question - Y or N.

I say just GO for it. OP you do have a backup, right? Please do let us know the result.


RandomStream
RandomStream
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3045 Visits: 648
jcelko212 32090 - Wednesday, January 3, 2018 8:02 PM
mcfarlandparkway - Wednesday, January 3, 2018 2:39 PM


>> I have a table, where it has status like active, terminated. <<

Where is the DDL for this table? Keys? Constraints? Data types? Did you read the basic forum rules before you posted?

I don’t you understand the basic principle of data modeling. Status is from the Latin word for a “state of being” And it implies that it has a temporal dimension. For example, if I had a properly named table instead of your vague generic data element name, such as “employment_status”, it’s values would depend on the time interval. If they were currently employed then the end of my interval would be a NULL to indicate that it has not completed yet.

Please read this article:

https://www.red-gate.com/simple-talk/sql/t-sql-programming/state-transition-constraints/

>>I have checked the duplicate records [sic] in the table with pn_id how many active and how many terminated are there.. I found only one record [sic].<<

Please learn the difference between a row and a record. This is a fundamental concept in RDBMS. Now it’s time to start guessing at your DDL and correcting it. Even though we can’t see it Sad

SELECT pn_id, COUNT(*)
FROM Active_Status --- Invalid table name
GROUP BY pn_id HAVING COUNT(*) > 1;

You don’t understand what a table is! The “active status” is not a set of entities or a relationship, but is a value of an attribute inside a table.

-- I found only one pn_id which has active and terminated record [sic].

>> Is this query [sic]] correct to delete records [sic]?<<

This is a statement, not a query.


This attitude reminds me of my college days. Wait a minute, Professor Celko? Is that really you!? Doh!
ManicStar
ManicStar
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15819 Visits: 6017
RandomStream - Thursday, January 4, 2018 5:55 PM
jcelko212 32090 - Wednesday, January 3, 2018 8:02 PM
mcfarlandparkway - Wednesday, January 3, 2018 2:39 PM


>> I have a table, where it has status like active, terminated. <<

Where is the DDL for this table? Keys? Constraints? Data types? Did you read the basic forum rules before you posted?

I don’t you understand the basic principle of data modeling. Status is from the Latin word for a “state of being” And it implies that it has a temporal dimension. For example, if I had a properly named table instead of your vague generic data element name, such as “employment_status”, it’s values would depend on the time interval. If they were currently employed then the end of my interval would be a NULL to indicate that it has not completed yet.

Please read this article:

https://www.red-gate.com/simple-talk/sql/t-sql-programming/state-transition-constraints/

>>I have checked the duplicate records [sic] in the table with pn_id how many active and how many terminated are there.. I found only one record [sic].<<

Please learn the difference between a row and a record. This is a fundamental concept in RDBMS. Now it’s time to start guessing at your DDL and correcting it. Even though we can’t see it Sad

SELECT pn_id, COUNT(*)
FROM Active_Status --- Invalid table name
GROUP BY pn_id HAVING COUNT(*) > 1;

You don’t understand what a table is! The “active status” is not a set of entities or a relationship, but is a value of an attribute inside a table.

-- I found only one pn_id which has active and terminated record [sic].

>> Is this query [sic]] correct to delete records [sic]?<<

This is a statement, not a query.


This attitude reminds me of my college days. Wait a minute, Professor Celko? Is that really you!? Doh!

(watching the drama train chug on by)

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search