[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1460
  • Last Modified:

SQL Syntax to Find and Count Duplicates of concatenated fields where Count > 1

I have a table Tbl1 with the following fields:
ID (INT)
A  (varchar)
B  (INT)
C  (varchar)
D  (varchar)

Typical values are:
ID          A          B            C              D
1          aaa       1           xxxx          yyyy
2          bbb       1           dddd         eeee
3          aaa       1           xxxx          yyyy
4          ccc         2          hhhh         gggg
5          aaa       2           hhhh         gggg

I need SQL Syntax to Select the count of entries, grouped by A, where the concatenation of B & C & D is identical and where the count of such entries is greater than 1.

The result of the query in this case would be a single row:

A             Count_Of_Duplicates
aaa                   2
0
wsturdev
Asked:
wsturdev
  • 3
  • 3
  • 2
2 Solutions
 
MikeTooleCommented:
I think that this should do it

Select A, Count(A)
From ...
group by B + C + D
Having Count( B + C + D) > 1
0
 
wsturdevAuthor Commented:
Don't mean to be so dense, but what is
<From ...>?
0
 
MikeTooleCommented:
<From ...>
From YourTable Where somefield = something

Basically ... indicates that something was left out, i.e your table name and any Where clause you may need.

I've also assumed that your SQL database engine uses a '+' to join strings together.  
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
imitchieCommented:

SELECT A, COUNT(*) AS Count_Of_Duplicates
FROM Tbl1
GROUP BY A, B+C+D
HAVING COUNT(*) > 1

Open in new window

0
 
imitchieCommented:
yes which database are you using please?
0
 
wsturdevAuthor Commented:
SQL Server
0
 
imitchieCommented:
for MS Access, use this
SELECT A, COUNT(*) AS Count_Of_Duplicates
FROM Table2
GROUP BY A, B & C & D
HAVING COUNT(*) > 1

Open in new window

0
 
wsturdevAuthor Commented:
MIkeToole and imitchie, I am going to split the points between you.  YOur combined responses got me to investigating further.

This is what finally worked:
SELECT A, COUNT(*) AS Duplicate_Count FROM Tbl1 tbl WHERE EXISTS
(SELECT Count(ID), (CAST(B AS varchar(20)) + C + D) AS ComparisonVal FROM Tbl1
WHERE (CAST(B AS varchar(20)) + C + D) = (CAST(tbl.B AS varchar(20)) + tbl.C + tbl.D)
AND A = tbl.A  
GROUP BY (CAST(B AS varchar(20))+C+D)
HAVING COUNT(ID)>1)
GROUP BY A
ORDER BY A
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now