I am trying to write a query in SQL server to find out if there are any multiple rows for each hash value.
I need all filenames where the hash value has duplicates.
The result should be (based on my example below)
003B4C68BC143B0290E04432A3A96092 File0003.jpg
003B4C68BC143B0290E04432A3A96092 File0004.jpg
003B4C68BC143B0290E04432A3A96092 File0005.jpg
Please let me know.
Here is the table structure
File table
-----------------------------------------
hash FileName
---------------------------------------
000341A486F5492877D588BED0806650 File0001.jpg
00363EF2ECEEA32F10176EB64A50283F File0002.jpg
003B4C68BC143B0290E04432A3A96092 File0003.jpg
003B4C68BC143B0290E04432A3A96092 File0004.jpg
003B4C68BC143B0290E04432A3A96092 File0005.jpg
One way to find duplicate records from the table is the GROUP BY statement. The GROUP BY statement in SQL is used to arrange identical data into groups with the help of some functions. i.e if a particular column has the same values in different rows then it will arrange these rows in a group.
To find the duplicate Names in the table, we have to follow these steps: Defining the criteria: At first, you need to define the criteria for finding the duplicate Names. You might want to search in a single column or more than that. Write the query: Then simply write the query to find the duplicate Names.
select *
from File
where hash in (select
hash
from File
group by hash
having count(*) > 1)
You can use EXISTS
to check for duplicates,
SELECT a.*
FROM TableName a
WHERE EXISTS
(
SELECT 1
FROM Tablename b
WHERE a.hash = b.hash
GROUP BY hash
HAVING COUNT(*) > 1
)
or INNER JOIN
SELECT a.*
FROM [File] a
INNER JOIN
(
SELECT hash
FROM [File] b
GROUP BY hash
HAVING COUNT(*) > 1
) b ON a.hash = b.hash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With