Solved

SQL Statement - Can't add ID

Posted on 2011-03-10
3
345 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 500 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

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!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

735 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