Solved

Creating SQL View - Combining rows into one comma delimited row

Posted on 2014-03-27
2
362 Views
Last Modified: 2014-03-27
Hi. I am attempting to create views that display data with rows combined into a single comma delimited row - each have unique record identifiers. I've attached the code I have been working with and an example. I don't think it should be too difficult, I just can't figure it out. Thank you!
Creating-SQL-View.docx
0
Comment
Question by:LCNW
[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
2 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39960312
You need to limit the data being passed into stuff()
select T1.CaseFileIdentifier,
       STUFF(
              (select ','+convert(varchar(MAX),NoteText)
               from dbo.AllNotes T2
               where T2.CaseFileIdentifier = T1.CaseFileIdentifier
               and t2.NoteType = 'Update'
               for xml Path('')
       ), 1, 1, '') as NoteText                
  from dbo.AllNotes T1
  --where t1.NoteType = 'Update'
 group by T1.CaseFileIdentifier

---------------------------
    INSERT INTO AllNotes
    	([CaseFileIdentifier], [NoteType], [NoteText])
    VALUES
    	(123, 'Update', 'Cow'),
    	(123, 'Update', 'Horse'),
    	(123, 'Assignment', 'Cat'),
    	(456, 'Update', 'Dog'),
    	(456, 'Assignment', 'Chicken'),
    	(456, 'Assignment', 'Duck')
    ;

**Query 1**:

    select T1.CaseFileIdentifier,
           STUFF(
                  (select ','+convert(varchar(MAX),NoteText)
                   from dbo.AllNotes T2
                   where T2.CaseFileIdentifier = T1.CaseFileIdentifier
                   and t2.NoteTYpe = 'Update'
                   for xml Path('')
           ), 1, 1, '') as NoteText                
      from dbo.AllNotes T1
      --where t1.NoteType = 'Update'
     group by T1.CaseFileIdentifier
    

**[Results][2]**:
    
    | CASEFILEIDENTIFIER |  NOTETEXT |
    |--------------------|-----------|
    |                123 | Cow,Horse |
    |                456 |       Dog |



  [1]: http://sqlfiddle.com/#!3/7282cd/3

Open in new window

0
 
LVL 1

Author Closing Comment

by:LCNW
ID: 39960374
Perfect! Thank you!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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