Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help in removing the duplicates in the table

Author  Topic 

vidyar
Starting Member

2 Posts

Posted - 2014-03-21 : 05:59:20
The database has Name,Email, and skill. Though the name is distinct it is repeated as it has different skills. I would like to remove duplicate names and add the corresponding skill to the only one row.

From the stored procedure, combining 3 tables I got the output as:

Name email department Skill
Arun emailid Tech team Technical
Arun emailid Tech team Leadership
Arun emailid Tech team Decision Making
Binay emailid Marketing Technical
Binay emailid Marketing Decision Making

I would like to remove the duplicate Name fileds and combine the Skill in a single row as other fields are same.

So the output should be
Name email department Skill
Arun emailid Tech team Technical, Leadership, Decision Making
Binay emailid Marketing Technical,Decision Making

Please let me know how can I proceed?


James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-03-23 : 18:10:08
[code]
SELECT
a.name,
a.email,
a.department,
STUFF(b.departments,1,1,'') AS Departments
FROM
(SELECT DISTINCT NAME, email, department FROM YourTable a) a
CROSS APPLY
(
SELECT ',' + Skill
FROM YourTable b
WHERE a.name = b.name AND a.email = b.email AND a.department = b.department
FOR XML PATH('')
) b(departments);[/code]
Go to Top of Page

vidyar
Starting Member

2 Posts

Posted - 2014-03-26 : 01:55:16
thanks James, Since i am new to SQL I was not able to update my code with the code that you have given. So i am providing all the details about my code so that you can understand my issue.

The stored procedure that is giving this result is:

SELECT distinct e.firstName + ' ' + e.lastName as name, e.email, e.geo,c.name as skill, d.name as department, e.addedDate, e.modifiedDate
from employee e
inner join mentor_offer m
on e.email=m.email
inner join department d
on e.department = d.id
inner join criteria c
on m.criteria_id = c.id
order by e.addedDate desc

Where Employee, mentor_offer, department and criteria are different tables.
Employee has all the employee details and department has the id and name of all the departments, Criteria has id and name of all the skills.

At present I get the result like this:


Name|Email|Location|Department|Skill|addedDate|mod ifiedDate
-----------------------------------------------------------
Arun Rao|arun@test.com|NA| Marketing|Planning & Organizing| 3/17/2014 1:01:21 PM|3/20/2014 2:37:50 PM
Arun Rao|arun@test.com|NA| Marketing|Technical Skills| 3/17/2014 1:01:21 PM|3/20/2014 2:37:50 PM
Binay|binay@test.com|NA|Sales|Leadership|3/17/2014 1:01:21 PM|3/20/2014 2:37:50 PM
Binay|binay@test.com|NA|Sales|Technical Skills|3/17/2014 1:01:21 PM|3/20/2014 2:37:50 PM

Expected Result: I would like to add the skills in a single row with comma separated and remove the duplicate entries of Name, Email ,Location, Department.
Name|Email|Location|Department|Skill|addedDate|mod ifiedDate
-----------------------------------------------------------
Arun Rao|arun@test.com|NA| Marketing|Planning & Organizing, Technical Skills|3/17/2014 1:01:21 PM|3/20/2014 2:37:50 PM
Binay|binay@test.com|NA|Sales|Leadership, Technical Skills|3/17/2014 1:01:21 PM|3/20/2014 2:37:50 PM

Please help.
Go to Top of Page
   

- Advertisement -