I have a table in which it contains duplicate data based on multiple columns.I am looking to select the matching data based on multiple columns and at the same time non matching based on another column.
CREATE TABLE #table1
(
id INT,
code VARCHAR(5),
type VARCHAR(5)
)
INSERT INTO #table1(id,code,type)
SELECT 1,'AA','T' UNION ALL
SELECT 2,'AB','R' UNION ALL
SELECT 3,'AC','T' UNION ALL
SELECT 1,'AA','K' UNION ALL
SELECT 3,'AC','T'
SELECT t.*
FROM #table1 T
INNER JOIN #table1 T2
ON t.id = t2.id
AND t.code = t2.code
AND t.type <> t2.type
I tried by self join with matching fields equal and non matching fields not equal
1条答案
按热度按时间ulydmbyx1#
Try this:
This query will return the rows from
Table1
that have matchingID
andCODE
values but differentTYPE
values.