Solved

Merge two rows in SQL

Posted on 2010-11-15
2
711 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 143

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What can the "OPTION" in the GROUP help me in my query? 4 35
find SQL job run average duration 24 53
Can I skip a node in XML? 9 29
Display field if column exists 7 30
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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