MS SQL  Rows to Columns with unknown # columns

Posted on 2013-05-26
Last Modified: 2013-05-28
I have a table that contains payment transactions for invoices.  Invoices can have an unknown # of payments.

ID   Invoice#     PaymentAmount    PaymentDate
1      102            100.45                     1/1/2012
2      102            95.45                       1/2/2012
3      102            50.45                       1/3/2012
4      103            259.00                     1/1/2012
5      104            24.00                       1/9/2012

I need to present the data similar to below:

Invoice#            Amount1     Date1           Amount2    Date2
(amount and date columns for each payment for the invoice)
102                     100.45        1/1/2012      95.45          1/2/1/2012   50.45      1/3/2012
103                      259.00       1/1/2012
104                      24.00         1/9/2012

I can accomplish this if I have a set number of amount/payments but I seem to be struggling with having dynamic columns.
Question by:jmichael18
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
LVL 45

Expert Comment

by:Kent Olsen
ID: 39197848
Hi Michael,

There are 3 approaches to this.  One is to join the tables together as many times as you need rows (you're doing this now).  Another is to write a UDF to take the rows and concatenate the data.  A third is to use recursive SQL.  There are a couple of articles on the DB2 forum that describe recursive SQL, and the examples are almost exactly what you're looking for.  The differences between DB2 SQL and Microsoft SQL are minor.

Take a look at that.  If it looks like something that you want to try, I'll be glad to help you with it.  It's really very flexible, and once you've done it a couple of times it's easy.

LVL 48

Expert Comment

ID: 39198233
this approach might work for you, in this example the dates are used as column headings like this
INVOICE  01/01/12   01/02/12 01/03/12   01/09/12
102      100        95       50         (null)
103      259       (null)   (null)      (null)
104      (null)    (null)   (null)      24

Open in new window


SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(convert(varchar, PaymentDate ,1) ) 
            FROM payments
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 

set @query = 'SELECT Invoice, ' + @cols + ' from 
                select Invoice, PaymentAmount, PaymentDate
                from Payments
           ) x
                for PaymentDate in (' + @cols + ')
            ) p '


Open in new window

LVL 48

Accepted Solution

PortletPaul earned 500 total points
ID: 39198556
I've come up with a dynamic approach for this, but I want to stress that because it is dynamic it needs you to amend the code in 2 places to implement the "filter" of the data. It produces output in this fashion:
102      100    01/01/12   95     01/02/12  50      01/03/12
103      259    01/01/12  (null)  (null)   (null)   (null)
104      24    01/09/12   (null)  (null)   (null)   (null)

Open in new window

see it working at:!3/807c6/2

Here is the code, and I have indicated where the "filter" should be included

COLS as (
              ', max(CASE WHEN row_ref = ' 
            + convert(varchar,number) 
            + ' THEN PaymentAmount ELSE null END) AS Amount' 
            + convert(varchar,number)
            + ', max(CASE WHEN row_ref = ' 
            + convert(varchar,number) 
            + ' THEN convert(varchar, PaymentDate ,1) ELSE null END) AS Date' 
            + convert(varchar,number)
            as casea
          FROM master.dbo.spt_values
          WHERE  type = 'P'
          AND number BETWEEN 1 AND (
                                    SELECT max(c)
                                    FROM (
SELECT count(*) AS c
FROM payments
where 1=1 /* i.e. filter the data here */
GROUP BY invoice
                                        ) as x

select @query = 

+ 'WITH CTE as ('
+ '        select'
+ '            Invoice'
+ '          , PaymentAmount'
+ '          , PaymentDate'
+ '          , row_number() over (partition by invoice order by paymentDate) as row_ref'
+ '        from Payments'
+ '        where 1=1 /* i.e. filter the data here */'
+ '       )'
+ 'select invoice, ' 
           ( SELECT casea
             FROM cols
             FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
+ ' from CTE group by invoice'


Open in new window


Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

734 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