Solved

Take multiple records and contain to one

Posted on 2014-04-19
8
108 Views
Last Modified: 2014-04-20
Please see my attached example

I have an output that requires multiple records to be displayed for several activities.  However, I was wondering if I can list all my activities in one record as my attached example shows.

Is there a way to do this in SQL without having multiple records in first example?
Sample.xlsx
0
Comment
Question by:al4629740
[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
  • 4
  • 4
8 Comments
 
LVL 27

Expert Comment

by:skullnobrains
ID: 40011028
SELECT STUFF(
             (SELECT ',' + Activity
              FROM Table_Name
              FOR XML PATH (''))
             , 1, 1, '')

you'll need to add require group by clauses and edit the table name.

note that this is specifically complicated in sql server as opposed for example to mysql where you can do this using group_concat in a simple select expression
0
 

Author Comment

by:al4629740
ID: 40011283
If my table is called table1,  can you write an example using the fields in the attached document?
0
 
LVL 27

Accepted Solution

by:
skullnobrains earned 500 total points
ID: 40011324
try this, and bare with me by trying to debug minor typos : i do not have an ms sql server around and hardly ever used one for years.

assuming you have several agencies, i added a where clause in the subquery.
this requires to rename the table in the subquery, which is why it is a little more complex than the example

i used max(FacilityName) and not facilityName because sql server returns an error if you don't use an aggregate with a group by

SELECT Agency,max(FacilityName),
        STUFF((    SELECT ',' + Activity AS [text()]
                          FROM table1 as sub
                         WHERE table1.Agency = sub.Agency
                         FOR XML PATH('')
                     ), 1, 1, '' ) AS [Activities]
FROM  table1
group by Agency
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:al4629740
ID: 40011744
Is it possible to select distinct activity in the code you had above?
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 40011760
i think both distinct or add it to the group by should work but i'm unsure. please post results if you give it a try
0
 

Author Comment

by:al4629740
ID: 40011783
Code used

SELECT [Committee Name],max(FacilityName),
        STUFF((    SELECT ',' + Activity AS [text()]
                          FROM frmUnregisteredEvent as sub
                         WHERE frmUnregisteredEvent.[Committee Name] = sub.[Committee Name]
                         FOR XML PATH('')
                     ), 1, 1, '' ) AS [Activities]
FROM  frmUnregisteredEvent
group by [Committee Name]



attached output
Book1.xlsx
0
 

Author Comment

by:al4629740
ID: 40011784
Notice column 3 has some duplicate activities.
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 40012059
i think i misunderstood previously : what you want is to prevent having things like "a,b,b,c" in that column.

you can use "select distinct ',' + Activity ... FOR XML PATH" in the inner query. afaik, it should provide the desired results.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SCOM to SQL port 14xx failed? 1 32
Need help with a query 14 39
SQL Server Express automatically execute SQL or SP 8 37
Trouble installing msi file with msiexe.exe 2 20
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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