Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2009 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > SQL Server > Multiple SQL rows merge into single row if the id is same ...
Muhsinathk

Multiple SQL rows merge into single row if the id is same

 Code Snippet posted by: Muhsinathk | Posted on: 6/14/2012 | Category: SQL Server Codes | Views: 1042 | Status: [Member] | Points: 40 | Alert Moderator   


Hi,

CREATE TABLE employee
(
LastName VARCHAR(20),
DepartmentID INT
);

INSERT INTO employee(LastName, DepartmentID) VALUES('Rafferty', 31);
INSERT INTO employee(LastName, DepartmentID) VALUES('Jones', 33);
INSERT INTO employee(LastName, DepartmentID) VALUES('Steinberg', 33);
INSERT INTO employee(LastName, DepartmentID) VALUES('Robinson', 34);
INSERT INTO employee(LastName, DepartmentID) VALUES('Smith', 34);
INSERT INTO employee(LastName, DepartmentID) VALUES('John', NULL);


SELECT e1.DepartmentID,
DepartmentID=replace((SELECT LastName AS [data()]
FROM employee e2 WHERE e2.DepartmentID=e1.DepartmentID ORDER BY DepartmentID
FOR XML PATH('')),' ',',')FROM employee e1 GROUP BY DepartmentID
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/18/2013 4:20:59 PM