Solved

Take multiple records and contain to one

Posted on 2014-04-19
8
106 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:al4629740
ID: 40011744
Is it possible to select distinct activity in the code you had above?
0
 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

773 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