?
Solved

MS SQL Columns To Rows

Posted on 2013-05-24
4
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 21

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

764 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