Solved

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

Posted on 2010-08-31
3
740 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
3 Comments
 
LVL 142

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now