SQL for 1:1 match of two columns
Given a table of two columns with recurring values in each column, how can I identify the rows where a 1:1 match between the two rows exists? E.g.
A A
B A
开发者_如何学GoC A
A B
F B
C C
C C
B A
C A
I want to pick C C.
I am using SQL Server 2000.
Thanks.
Group rows by column1, column2
and pick those having count > 1
I think this is a duplicate check, is it not? You want to find out any rows with duplicates on 2 columns?
So
select col1, col2 from table
where exists(select 1 from table tab2 where table.col1=tab2.col1
and table.col2=tab2.col2 and tab2.id <> table.id)
SELECT c1,c2,count(*) as c FROM tbl GROUP BY c1 HAVING c=1
Do you mean:
SELECT [COL1] , [COL2] FROM [TABLE_NAME] WHERE [COL1] = 'C' AND [COL2] = 'C'
EDIT:
SELECT [COL1] , [COL2] FROM [TABLE_NAME] WHERE [COL1] = [COL2]
EDIT 2:
So its a uniqueness thing then?
SELECT [COL1] , [COL2], COUNT(*) FROM [TABLE_NAME] WHERE [COL1] = [COL2] GROUP BY [COL1] , [COL2] HAVING COUNT(*) = 1
This? Its hard to understand exactly what your requirements are.
SELECT `COL1`, `COL2` FROM `TABLE` WHERE `COL1` = `COL2`
精彩评论