Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-31
3
Medium Priority
?
751 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 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

916 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