Solved

SQL Statement - Can't add ID

Posted on 2011-03-10
3
322 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
3 Comments
 
LVL 142

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 40

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

24 Experts available now in Live!

Get 1:1 Help Now