Solved

Creating SQL View - Combining rows into one comma delimited row

Posted on 2014-03-27
2
349 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 48

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 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