Solved

Merge two rows in SQL

Posted on 2010-11-15
2
707 Views
Last Modified: 2012-05-10
Hi,
we have created a view that shows info from many different tables joined together. the output look like this
SELECT * from view_Analysts
AS-IS result:
id  date              analyst  type    
1   10/10/2009   Smith     Lunch  
1   10/10/2009   Hall        Lunch  
2   12/11/2009   Smith     Lunch
3   13/11/2009   Jackson Group
3   13/11/2009   Harris     Group

TO-BE result
id  date              analyst              type
1   10/10/2009   Smith+Hall         Lunch
2   12/11/2009   Smith                 Lunch
3   13/11/2009   Jackson+Harris Group

When all columns, except analyst, is the same, then mergethe two rows into one row with a '+' sign in analyst column

How can I write my sql to get TO-BE output?
0
Comment
Question by:abgsc
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 34135144
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 250 total points
ID: 34135157
This would do:
select distinct t1.id, t1.date, t1.type,
substring((SELECT '+' + t2.analyst FROM view_Analysts t2 where t1.id = t2.id for XML PATH ('')), 2, 1000) analyst
FROM view_Analysts t1

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

15 Experts available now in Live!

Get 1:1 Help Now