I am using the following query to get duplicates in a table:
SELECT FirstName, LastName, Num, COUNT() AS NumberDuplicates
FROM Table1
GROUP BY FirstName, LastName, Num
HAVING (COUNT() > 1)
ORDER BY NumberDuplicates DESC
I want to add to this query where it will only return duplicates where a field calle RefNum contains data.
Anyone know how I could achieve this?
SELECT FirstName, LastName, Num, COUNT(*) AS NumberDuplicates
FROM Table1
WHERE RefNum IS NOT NULL
GROUP BY FirstName, LastName, Num
HAVING (COUNT(*) > 1)
ORDER BY NumberDuplicates DESC
When I try to add RefNum as a column to be returned like this and dont have it in the Group By I get an error message saying it RefNum must be contained in the Group By
whatever columns in the SELECT that is not in aggregate must appear i the GROUP BY
SELECT FirstName, LastName, Num, COUNT(*) AS NumberDuplicates
FROM Table1
GROUP BY FirstName, LastName, Num
HAVING (COUNT(*) > 1)
ORDER BY NumberDuplicates DESC
What I want is to return the duplicates in the table where one or other of the duplicate records contains a value in HGRefNum or not. Not all the duplicates will have a value in HGRefNum only one of them probably.
I just want to display the duplicate records and be able to see the HGRefNum field regardless of whether it holds data or not.
SELECT FirstName, LastName, COUNT(*) AS NumberDuplicates
FROM Table1
GROUP BY FirstName, LastName
HAVING (COUNT(*) > 1) AND (MAX(RefNum) IS NOT NULL)
ORDER BY NumberDuplicates DESC
; WITH DUP AS
(
SELECT FirstName, LastName, COUNT(*) AS NumberDuplicates
FROM Table1
GROUP BY FirstName, LastName
HAVING (COUNT(*) > 1)
)
SELECT d.*, t.RefNum
FROM DUP d
INNER JOIN Table1 t ON d.FirstName = t.FirstName
AND d.LastName = t.LastName
ORDER BY NumberDuplicates DESC
; WITH DUP AS (SELECT FirstName, LastName, Num, COUNT() AS NumberDuplicates
FROM table1
WHERE Num IS NOT NULL
GROUP BY FirstName, LastName, Num
HAVING (COUNT() > 1))
SELECT d .*, t .Num
FROM DUP d INNER JOIN
table1t t ON d .FirstName = t .FirstName AND d .LastName = t .LastName
WHERE (Num LIKE N'%HO%')
ORDER BY NumberDuplicates DESC
; WITH DUP
AS (
SELECT FirstName,
LastName,
Num,
COUNT(*) AS NumberDuplicates
FROM table1
WHERE Num IS NOT NULL
GROUP BY FirstName,
LastName,
Num
HAVING (COUNT(*) > 1)
)
INSERT INTO HDuplicates ( <THE COLUMN NAME HERE> )
SELECT <THE COLUMN NAME HERE>
FROM DUP d
INNER JOIN table1t t
ON d.FirstName = t.FirstName
AND d.LastName = t.LastName
WHERE (Num LIKE N'%HO%')
ORDER BY NumberDuplicates DESC
You are using SQL Server which version? This is a CTE where DUP is "declared" by the "WITH"
The starting ; really should be on the statement before and there should be a ; after the DESC
This is now working khtan
If I wanted to get the duplicates based on three fields and pnum is the additional field do I just add it at the inner join as d.pnum = t.pnum ?