Solved

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

Posted on 2007-11-26
8
1,451 Views
Last Modified: 2012-08-14
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
Comment
Question by:wsturdev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 27

Accepted Solution

by:
MikeToole earned 250 total points
ID: 20351443
I think that this should do it

Select A, Count(A)
From ...
group by B + C + D
Having Count( B + C + D) > 1
0
 
LVL 1

Author Comment

by:wsturdev
ID: 20352313
Don't mean to be so dense, but what is
<From ...>?
0
 
LVL 27

Expert Comment

by:MikeToole
ID: 20352356
<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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 25

Assisted Solution

by:imitchie
imitchie earned 250 total points
ID: 20352601

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

Open in new window

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20352604
yes which database are you using please?
0
 
LVL 1

Author Comment

by:wsturdev
ID: 20352653
SQL Server
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20352663
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
 
LVL 1

Author Comment

by:wsturdev
ID: 20352851
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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question