Solved

SQL Statement - Can't add ID

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 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

13 Experts available now in Live!

Get 1:1 Help Now