?
Solved

SQL Statement - Can't add ID

Posted on 2011-03-10
3
Medium Priority
?
354 Views
Last Modified: 2012-05-11
I have a statement that counts occurrences of a combined field. I need the ID number of each of those rows.
SELECT    sFirstName + ' ' + sLastName AS Contacts, COUNT(sFirstName + ' ' + sLastName) AS NumOccurrences
FROM         tContacts
GROUP BY sFirstName + ' ' + sLastName
HAVING      (COUNT(sFirstName + ' ' + sLastName) > 1)

Open in new window

0
Comment
Question by:mgordon-spi
[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 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35100875
you need to join back;
select c.sFirstName , c.sLastName, c.ID
  from tContacts c
  join ( SELECT    sFirstName , sLastName , COUNT(*) AS NumOccurrences
           FROM         tContacts
          GROUP BY sFirstName , sLastName
           HAVING     count(*) > 1
        ) l
  ON l.sFirstName = c.sFirstName
 AND l.sLastName = c.sLastName

Open in new window

0
 
LVL 41

Expert Comment

by:Sharath
ID: 35101160
What is your SQL Server version?
0
 

Author Closing Comment

by:mgordon-spi
ID: 35101197
That was great, thank you very much..
0

Featured Post

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

764 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