I haven’t had to work with SQL queries in quite a while and what seems like a simple query is driving me insane. I have a table of transactions from which I am trying to select repetition based on various criteria. The relevant table structure is as follows:
[Transaction]
* ID int
Transacted datetime
Name nvarchar(50)
Description nvarchar(4000)
size bigint
From this table, I need to find duplicates of the name / size and report back all records where the name had the same size transaction, even if date and description differ. I can get counts with just the name with the following:
SELECT Name, COUNT(Name) AS Count
FROM Transaction
GROUP BY Name
And then I could do an inner join on that to get the rest of the data:
SELECT data.ID, data.Transacted, data.Name, data.Description, data.Size, counts.Count
FROM (SELECT Name, COUNT(Name) AS Count
FROM Transaction
GROUP BY Name) AS counts
INNER JOIN Transaction AS data ON counts.Name = data.Name
WHERE (counts.Count > 1)
ORDER BY data.Name, data.Transacted
But obviously this is only giving me transactions where the name is repeated — not where both the name and size are repeated.
I’m looking for help finishing this piece off so that I report all of the data where the lines show a repeat of the same name and same size. I could easily concatinate the data from both columns into one result and compare against that, but I’m not sure that’s the optimal appropach.
Thanks in advance.
The code in the question indicates that you also need additional columns. To return all rows with duplicated
Name, sizeyou can use