Use UNION to compare the data of the two tables with distinct T.

Source: Internet
Author: User
Problem description: today is the first time data is run after the new system is launched. It needs to be compared with the tables run by the old system. In the same way, the project runs OK, and the new system reduces manual intervention and increases by 80 age points. First run the data in the old system and import it to a backup table. Run it again with the new system. The quantity is the same as that in the backup table. I think it's easy

Problem description: today is the first time data is run after the new system is launched. It needs to be compared with the tables run by the old system. In the same way, the project runs OK, and the new system reduces manual intervention and increases by 80 age points. First run the data in the old system and import it to a backup table. Run it again with the new system. The quantity is the same as that in the backup table. I think it's easy

Problem description:

Today is the first time data is run after the new system is launched. It needs to be compared with the tables run by the old system. In the same way, the project runs OK, and the new system reduces manual intervention and increases by 80 age points. First run the data in the old system and import it to a backup table. Run it again with the new system. The quantity is the same as that in the backup table. I feel a bit open. The following method is used to compare whether each row of data is the same:

SELECT

COUNT (0)

FROM

(

SELECT * FROM table_nm_bac

UNION

SELECT * FROM table_nm

)

The table table_nm_bac and table table_nm contain 10 thousand data records. The results of the preceding query are.

Stage: SQL SERVER 2008

Solution:

Because it is impossible to produce less union results than each table, the following method is used immediately:

SELECT

COLUMN_NM1,

COLUMN_NM2

...

FROM

Table_nm

EXCEPT

SELECT

COLUMN_NM1,

COLUMN_NM2

...

FROM

Table_nm_bac

GO

SELECT

COLUMN_NM1,

COLUMN_NM2

...

FROM

Table_nm_bac

EXCEPT

SELECT

COLUMN_NM1,

COLUMN_NM2

...

FROM

Table_nm

GO

In this way, the results of both queries are empty. I am relieved. Later, I checked the data again using the following query and concluded that it was because union removed the repeated data in the table:

Select count (0) FROM (
Select distinct * FROM table_nm)

GO

SELECT
COUNT (0)
FROM
(
SELECT * FROM table_nm
UNION
SELECT * FROM table_nm
)

GO

The data after DISTINCT is the same as the data after UNION.

Conclusion: It is better to compare whether the data in the two tables is consistent using the except t method, because the two union operations will remove the duplicate data in the table.

Related Article

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.