|
Home > Archive > Microsoft SQL Server forum > January 2006 > This query must be easy but need help
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
This query must be easy but need help
|
|
| gwhite1@kc.rr.com 2006-01-19, 3:24 am |
| I have a table like this:
Name, SSN
Joe Smith, 1111
Tom Why, 2222
Larry Sam, 3333
Paul Tom, 4444
Steve bob, 1111
I want a query to pull off
Joe Smith, 1111
Steve bob, 1111
because someone accidently put in two different names with the same
SSN. There should only be one 1111 in the SSN field in the whole
database. I want to pull the duplicate SSN with the name. How can one
query do this. I can write a VB program to do it but I think a query
should work.
I know how to do this:
SELECT SSN, COUNT(*) AS cnt
FROM test
GROUP BY fSSN
HAVING (COUNT(*) > 1)
to find the duplicate SSN but I need the name listed with the SSN also.
Any help?? Thanks!!
Sheila
| |
|
| try this one:
select test.Name, test.SSN
from
test
inner join (select SSN from test group by SSN having count(*)>1) t2 on
test.ssn = t2.ssn
<gwhite1@kc.rr.com> wrote in message
news:1137647549.440302.217140@g49g2000cwa.googlegroups.com...
>I have a table like this:
>
> Name, SSN
> Joe Smith, 1111
> Tom Why, 2222
> Larry Sam, 3333
> Paul Tom, 4444
> Steve bob, 1111
>
> I want a query to pull off
>
> Joe Smith, 1111
> Steve bob, 1111
>
> because someone accidently put in two different names with the same
> SSN. There should only be one 1111 in the SSN field in the whole
> database. I want to pull the duplicate SSN with the name. How can one
> query do this. I can write a VB program to do it but I think a query
> should work.
>
> I know how to do this:
>
> SELECT SSN, COUNT(*) AS cnt
> FROM test
> GROUP BY fSSN
> HAVING (COUNT(*) > 1)
>
> to find the duplicate SSN but I need the name listed with the SSN also.
> Any help?? Thanks!!
>
> Sheila
>
| |
| gwhite1@kc.rr.com 2006-01-19, 8:24 pm |
| It worked perfect! Thanks for your help!!
| |
| --CELKO-- 2006-01-22, 3:23 am |
| Don't forget to add a constraint to the table so this will not happen
again.
|
|
|
|
|