Solved

Creating SQL View - Combining rows into one comma delimited row

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server merge records in one table 2 24
Count with a subquery showing details 10 44
SSRS  - Dropdown with Null 3 25
Why I am having this error in my query? 2 15
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

825 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