Solved

Take multiple records and contain to one

Posted on 2014-04-19
8
104 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
  • 4
  • 4
8 Comments
 
LVL 26

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 26

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
 

Author Comment

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

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 26

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 26

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

758 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

21 Experts available now in Live!

Get 1:1 Help Now