Solved

Query to combine two rows/columns of data into one row/column

Posted on 2010-08-31
3
744 Views
Last Modified: 2012-08-14
Hello, I have a view that yields these 3 rows of data.
FirstNameLastName HouseholdIDCustomerID  ProgramName
Joe           Schmoe              76           37      HUD SUP HOUSING
Joe              Schmoe           76              37      CHILD DAY CARE
Martin      VanBuren      73              32      HEAD START FEDERAL      

I want to combine the first two rows into one, so it looks like this:
                                                                          Programs
Joe           Schmoe              76            37      HUD SUP HOUSING, CHILD DAY CARE  

How would I do this?

I tried the stuff command but it yields the same result as the original dataset:

"select CUST.FirstName, CUST.LastName,
CUST.HouseholdID, CUST.CustomerID,
stuff((select ',' + [ProgramName] from CSE_Program PROG
where CUST.ProgramID = PROG.ProgramID AND
CUST.CustomerID = Cust.CustomerID
for XML PATH('')),1,1,'') as Programs, CUST.ProgramID
FROM VW_CustomersWithPrograms CUST "
0
Comment
Question by:dantcho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33569303
this is done with a "group_concat" function, in sql server with FOR XML PATH :
http://blog.shlomoid.com/2008/11/emulating-mysqls-groupconcat-function.html
0
 
LVL 6

Accepted Solution

by:
anushahanna earned 500 total points
ID: 33569434
With same data, see attached code
create table a (FirstName varchar(100), Lastname  varchar(100), HouseholdID  int, CustomerID Int, ProgramName varchar(100))

insert into a
select 'Joe','Schmoe',76,37,'HUD SUP HOUSING' union
select 'Joe','Schmoe',76,37,'CHILD DAY CARE' union
select 'Martin','VanBuren',73,32,'HEAD START FEDERAL'
WITH CustomersWithPrograms AS
(
select FirstName, LastName,HouseholdID, CustomerID,ProgramName FROM a  
)
SELECT DISTINCT FirstName, LastName,HouseholdID, CustomerID,
  NULLIF(   
    STUFF(   
      (SELECT ', ' + ProgramName FROM CustomersWithPrograms WHERE FirstName = Cust.FirstName
AND LastName = Cust.LastName FOR XML PATH('')  
                        ), 1, 2, ''   
    ), ''   
  ) AS AllCust 
FROM CustomersWithPrograms Cust

Open in new window

0
 

Author Closing Comment

by:dantcho
ID: 33569511
Perfect, exactly what I needed.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question