sql novice: how to find only duplicates
-
SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6582 row(s) affected
SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
-
SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6582 row(s) affected
SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
Try to union all by three selects like:
select distinct col1 from mytable
union all
select distinct col2 from mytable
union all
select distinct col3 from mytable
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6582 row(s) affected
SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
You can do this using a Group By clause
SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
FROM MyTable
GROUP BY Col1,Col2,Col3
ORDER BY DuplicateCount DESCAt this point you will have all the records, with the duplicates (Those with a count>1) at the beginning of the list. Interestingly, you can reduce this to JUST the duplicates by replacing the ORDER BY with a HAVING clause
SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
FROM MyTable
GROUP BY Col1,Col2,Col3
HAVING COUNT(*)>1Now you only have the duplicates.
-
SELECT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6582 row(s) affected
SELECT DISTINCT Col1, Col2, Col3 FROM MyTable ORDER BY Col1, Col2, Col3;
6473 rows(s) affected How can I get just the 109 rows that are duplicates? ie: the opposite of DISTINCT? Thanks
You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!
You're next question will be how do I remove the duplicates? Look into
row_number()
andpartition over
in BOLNever underestimate the power of human stupidity RAH
-
Try to union all by three selects like:
select distinct col1 from mytable
union all
select distinct col2 from mytable
union all
select distinct col3 from mytable
I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com
-
You can do this using a Group By clause
SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
FROM MyTable
GROUP BY Col1,Col2,Col3
ORDER BY DuplicateCount DESCAt this point you will have all the records, with the duplicates (Those with a count>1) at the beginning of the list. Interestingly, you can reduce this to JUST the duplicates by replacing the ORDER BY with a HAVING clause
SELECT Col1,Col2,Col3, COUNT(*) as DuplicateCount
FROM MyTable
GROUP BY Col1,Col2,Col3
HAVING COUNT(*)>1Now you only have the duplicates.
-
You're next question will be how do I remove the duplicates? Look into
row_number()
andpartition over
in BOLNever underestimate the power of human stupidity RAH