|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
One Unique FieldI have a table with 4 fields, that can contain duplicates.
I have to build a new table where field3 is the primary key, no duplicates. How do I write the SQL to return all the rows only were filed3 is unique? Thanks AJS > How do I write the SQL to return all the rows only were filed3 is One method:> unique? SELECT col1, col2, col3, col4 FROM dbo.MyTable WHERE col3 IN ( SELECT col3 FROM dbo.MyTable GROUP BY col3 HAVING COUNT(*) = 1 ) -- Show quoteHide quoteHope this helps. Dan Guzman SQL Server MVP <anth***@station5.net> wrote in message news:1163710137.747671.280270@m7g2000cwm.googlegroups.com... >I have a table with 4 fields, that can contain duplicates. > I have to build a new table where field3 is the primary key, no > duplicates. > > How do I write the SQL to return all the rows only were filed3 is > unique? > > Thanks > AJS > Thanks, It does help.
Dan Guzman wrote: Show quoteHide quote > > How do I write the SQL to return all the rows only were filed3 is > > unique? > > One method: > > SELECT col1, col2, col3, col4 > FROM dbo.MyTable > WHERE col3 IN > ( > SELECT col3 > FROM dbo.MyTable > GROUP BY col3 > HAVING COUNT(*) = 1 > ) > > > -- > Hope this helps. > > Dan Guzman > SQL Server MVP > > <anth***@station5.net> wrote in message > news:1163710137.747671.280270@m7g2000cwm.googlegroups.com... > >I have a table with 4 fields, that can contain duplicates. > > I have to build a new table where field3 is the primary key, no > > duplicates. > > > > How do I write the SQL to return all the rows only were filed3 is > > unique? > > > > Thanks > > AJS > >
Other interesting topics
can someone login as "NT AUTHORITY\SYSTEM "
Need help troubleshooting a memory paging issue List of running stored procedures TLog out of control limited permissions on test server for developers Spid checker query help Low Disk Space Delete records in a table with 15 dependencies how to remove the sql server registry mess? Remote Production Database Issue |
|||||||||||||||||||||||