Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008 R2: Remove single occurrence record.

I have the following table with two columns as shown below:

CREATE TABLE test_lin
(
    Cola INT,
    Colb INT
);

INSERT INTO test_lin VALUES(1,2);
INSERT INTO test_lin VALUES(1,3);
INSERT INTO test_lin VALUES(1,4);
INSERT INTO test_lin VALUES(1,5);
INSERT INTO test_lin VALUES(1,3);
INSERT INTO test_lin VALUES(2,4);
INSERT INTO test_lin VALUES(2,6);
INSERT INTO test_lin VALUES(2,7);
INSERT INTO test_lin VALUES(2,4);
INSERT INTO test_lin VALUES(2,6);

Note: Now I want to show only those records which are repeated more then once. Like in my case (1,3),(2,4),(2,6) records are repeated in the table.

enter image description here

I want to remove the single occurrence records from the result set. The records which are single occurrence are as shown below in the image.

enter image description here

like image 598
MAK Avatar asked Mar 12 '26 04:03

MAK


1 Answers

WITH CTE as 
(
SELECT Cola,Colb,ROW_NUMBER() OVER(PARTITION BY Cola,Colb ORDER BY Cola) AS RN
FROM test_lin
)

SELECT T.* 
FROM test_lin T INNER JOIN  CTE C ON C.COLa = t.Cola and C.COLb = t.Colb and c.RN=2
like image 79
Sateesh Pagolu Avatar answered Mar 13 '26 17:03

Sateesh Pagolu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!