Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Creating SQL View - Combining rows into one comma delimited row

Posted on 2014-03-27
2
Medium Priority
?
380 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
2 Comments
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

782 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