I currently have the following SQL server query:
SELECT * FROM
(
SELECT CustomerID,LEFT(Time,4) Month,SUM(Amount) Amount FROM Payment
Group by CustomerID,Time
) as s
PIVOT
(
SUM(Amount)
For [Month] In (jan, feb, mar, apr,
may, jun, jul, aug, sep, oct, nov, dec)
)AS pvt
This returns the following results:
CustomerID jan feb mar apr may jun jul aug sep oct nov dec
3482 NULL NULL NULL NULL 120 NULL 195 NULL NULL NULL NULL NULL
30249 NULL NULL NULL NULL NULL NULL NULL 70 NULL 35 NULL NULL
1073743410 NULL NULL NULL 60 NULL NULL 530.5 NULL 653.95 NULL NULL 20
1073743628 NULL NULL NULL NULL NULL 50 57.46 NULL NULL NULL 35 NULL
1073743632 NULL NULL NULL NULL NULL NULL 50 NULL NULL NULL NULL NULL
In this example the columns "jan", "feb" and "mar" have no results (all values are NULL) and so they shouldn't show. However when I run the query I'm not sure which columns will or will not be empty. Sometimes "oct" may have no results whilst jan will have results.
Can anyone inform me how I would get the results to dynamically only show the columns that have values?
Thank you