Solved

Creating SQL View - Combining rows into one comma delimited row

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now