If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Display Duplicates which have a different state

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-29-12, 18:55
Dorf Dorf Dorf Dorf is offline
Registered User
 
Join Date: Jan 2012
Posts: 7
Display Duplicates which have a different state

Hi I have this query here which displays all duplicates which have the same phone number but i am trying to modify it to display only fields that have the same phone number but have different state

SELECT *
FROM businesses t1
join (SELECT ce_phone FROM businesses GROUP BY ce_phone HAVING COUNT(*)>1) t2
ON t1.ce_phone = t2.ce_phone
WHERE t1.ce_phone is not null
ORDER BY t1.ce_phone

So what I would try to do would be to group state within the ce_phone group and count the state duplicates and if it is less than the count of phone then i would display it but i am just not sure how to write the syntax for it.

If anyone can set me in the right direction it would be much appreciated.
Reply With Quote
  #2 (permalink)  
Old 01-29-12, 19:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT ce_phone 
  FROM businesses 
GROUP 
    BY ce_phone 
HAVING COUNT(*) > 1
   AND COUNT(DISTINCT state) > 1
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-29-12, 19:50
Dorf Dorf Dorf Dorf is offline
Registered User
 
Join Date: Jan 2012
Posts: 7
Thankyou r937 that worked well
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On