I have done this:
;WITH a AS
(
SELECT
a.account
,index_num_date = 'date ' + CONVERT(VARCHAR(30), DENSE_RANK() OVER ( PARTITION BY a.account ORDER BY ratechangedate ))
,index_num_rate = 'rate ' + CONVERT(VARCHAR(30), DENSE_RANK() OVER ( PARTITION BY a.account ORDER BY ratechangedate ))
,ratechangedate
,new_noterate
FROM MARS_DW.dbo.vw_GTMScheduledRateAndPaymentChangesWithAccountNumber_Archive a
INNER JOIN
(
SELECT *
FROM mars..vw_loans
WHERE loanstatus <> 'bk payment plan'
) b ON a.account = b.account
WHERE archivedate = '5/20/2019'
)
,q1 AS
(
SELECT DISTINCT
account
,ratechangedate
,index_num_date
FROM a
)
,q2 AS
(
SELECT DISTINCT
account
,new_noterate
,index_num_rate
FROM a
)
,datepivot AS
(
SELECT DISTINCT
account
,[date 1]
,[date 2]
,[date 3]
,[date 4]
,[date 5]
,[date 6]
,[date 7]
,[date 8]
,[date 9]
,[date 10]
,[date 11]
,[date 12]
,[date 13]
FROM q1
PIVOT
(
MIN(ratechangedate)
FOR index_num_date IN ( [date 1]
,[date 2]
,[date 3]
,[date 4]
,[date 5]
,[date 6]
,[date 7]
,[date 8]
,[date 9]
,[date 10]
,[date 11]
,[date 12]
,[date 13]
)
) pvt1
)
,ratepivot AS
(
SELECT DISTINCT
account
,[rate 1]
,[rate 2]
,[rate 3]
,[rate 4]
,[rate 5]
,[rate 6]
,[rate 7]
,[rate 8]
,[rate 9]
,[rate 10]
,[rate 11]
,[rate 12]
,[rate 13]
FROM q2
PIVOT
(
MIN(new_noterate)
FOR index_num_rate IN ( [rate 1]
,[rate 2]
,[rate 3]
,[rate 4]
,[rate 5]
,[rate 6]
,[rate 7]
,[rate 8]
,[rate 9]
,[rate 10]
,[rate 11]
,[rate 12]
,[rate 13]
)
) pvt2
)
SELECT
a.Account
,[date 1]
,[rate 1]
FROM datepivot a
LEFT JOIN ratepivot b ON a.Account = b.Account
Which gives me this
But this is not dynamic and my Microsoft version 2016 does not allow me to use the Fiddle thing recommended in various posts. Thus I was recommended to use the Coalesce()
function but I have no idea what to do to make the latter dynamic. Any help (not referring to some post) would really help.
Update:
Following the comment I tried this:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.Account)
FROM MARS_DW.[dbo].[vw_GTMScheduledRateAndPaymentChangesWithAccountNumber_Archive] c
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Account, ' + @cols + ' from
(
select Account
, ratechangedate
, new_noterate
from MARS_DW.[dbo].[vw_GTMScheduledRateAndPaymentChangesWithAccountNumber_Archive]
) x
pivot
(
min(ratechangedate)
for category in (' + @cols + ')
) p '
but get this error:
Msg 1056, Level 15, State 1, Line 37
The number of elements in the select list exceeds the maximum allowed number of 4096 elements.
Msg 102, Level 15, State 1, Line 43
Incorrect syntax near 'x'.
Update:
I tried this to limit the amount
DECLARE @cols AS NVARCHAR(MAX);
DECLARE @query AS NVARCHAR(MAX);
SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.RateChangeDate)
FROM MARS_DW.[dbo].[vw_GTMScheduledRateAndPaymentChangesWithAccountNumber_Archive] c
WHERE c.ArchiveDate = '5/21/2019' AND c.AppliedDate > '1/2/2018'
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT Account, ' + @cols + ' from
(
select Account
, ratechangedate
, new_noterate
from MARS_DW.[dbo].[vw_GTMScheduledRateAndPaymentChangesWithAccountNumber_Archive]
) x
pivot
(
min(ratechangedate)
for category in (' + @cols + ')
) p
pivot
(
min(new_noterate)
for category in (' + @cols + ')
)
'
execute(@query)
But I am getting this error:
Msg 102, Level 15, State 1, Line 52
Incorrect syntax near ')'.
As requested here is the top 10 from the data table