Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS SQL Columns To Rows

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

618 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