Database null row caused by "deleting duplicate rows with one statement"

Source: Internet
Author: User
-- Duplicate null columns can be detected.
Delete From [ Jcxs0201 ] Where User_id In ( Select User_id U From [ Jcxs0201 ] Group By User_id Having Count (ID) > 1 ) And ID Not In ( Select Min (ID) From [ Jcxs0201 ] Where User_id In ( Select User_id From [ Jcxs0201 ] Group By User_id Having Count (ID) > 1 ))

 

-- Duplicate null Columns cannot be detected.
Delete From [ Jcxs0201 ] Where User_id In ( Select User_id U From [ Jcxs0201 ] Group By User_id Having Count (ID) > 1 ) And ID Not In ( Select Min (ID) From [ Jcxs0201 ] Group By User_id Having Count (ID) > 1 )

 

PS: later repeated tests/2012 10 18

Found in (null ,~) Null is invalid, which is equivalent to in (~)

So here is a problem: If the column contains a null value in the group by column

It can only be described by a non-null value, such as the ID of the modified row or the row number obtained by the row_number () function.

 

Analysis of the preceding statement errors:

It is described in two parts: user_id and ID.

I. Check user_id first

1. The effects of Count (ID) and count (user_id) after group by user_id are different.

The following statement indicates that three rows of user_id are null, and count (null) seems invalid.

2. Make the userid in (null, 1257646) and user_id in (1257646) statements consistent, and the previous null will be ignored.

In fact, all results are user_id in (1257646 ).

Can it be understood that, in SQL Server, null is not a value, but a default status?

2. Check the group ID.

The statement containing grou by user_id does not know whether the select ID can be used.

If not, null cannot be processed. Therefore, the row_number () function must be used to add a row number to it.

This is my understanding. Please point out any errors

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.