Solved

MS SQL Columns To Rows

Posted on 2013-05-24
4
298 Views
Last Modified: 2013-05-24
I have a table ItemNotes (ItemID int, Note varchar(255),NoteDate datetime) in a MS SQL Server2005 database.  I need to have all of the note and notedate fields for an itemID combined into the same row.  It is unknown how many Notes a given item has.  I have been attempting a cursor or pivot but I cannot seem to get it working.  Any assistance is appreciated.
0
Comment
Question by:jmichael18
  • 2
4 Comments
 
LVL 21

Expert Comment

by:Tapan Pattanaik
ID: 39196093
can you share sample format of your table.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
ID: 39196127
Hi. What you are asking for is far from trivial, and instead of attempting to provide exactly what you have asked for this is an alternative suggestion. Rather than trying to provide an unknown number of columns (date/note, date/note, date/note etc.) could you survive with this information in a concatenated format (i.e. one large string column).

NOTES
[05/10/2013] <<note here>> [05/12/2013] <<note here>>. [05/20/2013] <<note here>> ...
please see this sqlfiddle http://sqlfiddle.com/#!3/580b4/2 which displays the result of the following 2 queries. The upper query embeds date into the string, the lower query does not.
select
  Items.id
, stuff((
          select case when nref = 1 then ' [' + x.ndate + '] ' + x.Note  else ' ' + x.Note end
          from (
                select
                  ItemID
                , convert(varchar,Notedate, 101) as ndate
                , Note
                , row_number() over (partition by itemID, convert(varchar,Notedate, 101) order by NoteDate) as nref
                from ItemNotes
               ) as x
           where x.ItemID = Items.ID
           FOR XML path('')
          ), 1, 1, '') as Notes
from Items
;

select
  Items.id
, stuff((
           select ' ' + ItemNotes.Note
           from ItemNotes
           where ItemNotes.ItemID = Items.ID
           FOR XML path('')
          ), 1, 1, '') as Notes
from Items
;

Open in new window

The following variant would place most recent notes first, see http://sqlfiddle.com/#!3/580b4/7 
select
  Items.id
, stuff((
          select top 100 percent
            case when nref = 1 then ' [' + x.ndate + '] ' + x.Note  else ' ' + x.Note end
          from (
                select
                  ItemID
                , convert(varchar,Notedate, 101) as ndate
                , NoteDate
                , Note
                , row_number() over (partition by itemID, convert(varchar,Notedate, 101) order by NoteDate) as nref
                from ItemNotes
               ) as x
           where x.ItemID = Items.ID
           order by NoteDate DESC
           FOR XML path('')
          ), 1, 1, '') as Notes
from Items
;

Open in new window

0
 

Author Closing Comment

by:jmichael18
ID: 39196162
Works great.  Thank you so much.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39196172
no problem, which one did you use? (with dates?)
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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

919 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

16 Experts available now in Live!

Get 1:1 Help Now