Solved

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

Posted on 2007-11-26
8
1,438 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now