|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, October 14, 2012 12:40 PM
Points: 2,
Visits: 3
|
|
Hi,
I have a table which looks like :
Name Region City Status N_Region N_City N_Status E_Region E_City E_Stauts Peter AMERICA NY A EMEA LDN U AMERICAS NY A SHAWN AMERICA NY A EMEA LDN U AMERICAS LA A SHAWN AMERICA NY A EMEA LDN U AMERICAS NY A TONY AMERICA NY A EMEA LDN U AMERICAS LA A TONY AMERICA NY A EMEA LDN U EMEA LDN U
I am looking for a query which will give me a line as it is if the Name is UNIQUE ( In above eg : Peter )
If the name is duplicated than to give where the
Region and City are unique to N_Region and N_City and to E_Region and E_City
So the result should be :
Name Region City Status N_Region N_City N_Status E_Region E_City E_Stauts Peter AMERICA NY A EMEA LDN U AMERICAS NY A SHAWN AMERICA NY A EMEA LDN U AMERICAS LA A TONY AMERICA NY A EMEA LDN U AMERICAS LA A
Any help would be much appreciated
Thanks
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 8:36 AM
Points: 334,
Visits: 706
|
|
| How are you determining which record is the "correct" record to return for the duplicates? For instance, how do you know you want the LA Shawn and not the NY Shawn? Are you just taking a random order?
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: 2 days ago @ 4:59 PM
Points: 960,
Visits: 1,924
|
|
Welcome to SQLServerCentral! In order to help you, you need to help us. You should post your DDL and sample data in a consumable format (a way on which we can just run the script). As it's your first time, I'll do it for you:
DECLARE @Test TABLE( Name varchar(50), Region varchar(50), City varchar(50), Status varchar(50), N_Region varchar(50), N_City varchar(50), N_Status varchar(50), E_Region varchar(50), E_City varchar(50), E_Stauts varchar(50)); INSERT @Test VALUES('Peter', 'AMERICA', 'NY', 'A ', 'EMEA ', 'LDN ', 'U ', 'AMERICAS ', 'NY ', 'A'), ('SHAWN', 'AMERICA', 'NY', 'A ', 'EMEA ', 'LDN ', 'U ', 'AMERICAS ', 'LA ', 'A'), ('SHAWN', 'AMERICA', 'NY ', 'A ', 'EMEA ', 'LDN ', 'U ', 'AMERICAS ', 'NY ', 'A'), ('TONY', 'AMERICA', 'NY ', 'A ', 'EMEA ', 'LDN ', 'U ', 'AMERICAS ', 'LA ', 'A'), ('TONY', 'AMERICA', 'NY ', 'A ', 'EMEA ', 'LDN ', 'U ', 'EMEA ', 'LDN ', 'U');
However, your description is not clear enough. If it's just a duplicate problem, you SHOULD look for other answers (I did that for you too) http://www.sqlservercentral.com/search/?q=duplicate+records
I am looking for a query which will give me a line as it is if the Name is UNIQUE That could be accomplished with this:
WITH UniqueNames AS( SELECT Name FROM @Test GROUP BY Name HAVING COUNT(*) = 1) SELECT t.* FROM @Test t JOIN UniqueNames u ON t.Name = u.Name;
The next part if the problem is the part I can't understand
If the name is duplicated than to give where the
Region and City are unique to N_Region and N_City and to E_Region and E_City
Can you explain more?
Luis C. Please don't trust me, test the solutions I give you before using them. Forum Etiquette: How to post data/code on a forum to get the best help
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Sunday, October 14, 2012 12:40 PM
Points: 2,
Visits: 3
|
|
|
|
|