Calculate the number of times that the two columns are equal to each other and remove duplicates.

Source: Internet
Author: User
-- Calculate the number of times that the two columns are equal to each other and remove duplicates.

If object_id ('[TB]') is not null drop table [TB]
Go
Create Table [TB] ([num1] varchar (10), [num2] varchar (10 ))
Insert [TB]
Select 'A', 'B' Union all
Select 'B', 'C' Union all
Select 'B', 'A' Union all
Select 'C', 'B' Union all
Select 'C', 'D' Union all

Select * from TB

Select T. num1, T. num2, count (*) as repeated
From (
Select num1, num2 from TB
Union all
Select num2, num1 from TB
) T
Where T. num1 <t. num2
Group by T. num1, T. num2

/*

Num1 num2
A B
B c
B
C B
C d

----------------------------

Num1 num2 (repeated)
A B 2
B C 2
C d 1

*/

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.