?
Solved

MS SQL Columns To Rows

Posted on 2013-05-24
4
Medium Priority
?
355 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 22

Expert Comment

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

Accepted Solution

by:
PortletPaul earned 2000 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 49

Expert Comment

by:PortletPaul
ID: 39196172
no problem, which one did you use? (with dates?)
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

862 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