Solved

MS SQL Columns To Rows

Posted on 2013-05-24
4
288 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
Comment Utility
can you share sample format of your table.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
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
Comment Utility
Works great.  Thank you so much.
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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

11 Experts available now in Live!

Get 1:1 Help Now