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
  • 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.

828 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