sql server – Get all details by date without aggregate functions with dynamic pivot

I have the following table and I’m trying to put the (Fecha de pago) rows as a column, and I found similar questions but they are using aggregate functions like:

Dynamic pivot: sum of sales per month

Dynamic dates Pivot

SQL Server dynamic PIVOT query?

Passing column names dynamically to UNPIVOT //This is similar but in reverse

Fracción Importación    Valor Comercial Importación Fecha de pago   Fraccion    Valor Comercial
85045099                4185787.29                  2015            85045099    307032.000000
84819099                419688356.99                2015            84819099    398684.000000
84212199                11248672.39                 2015            84212199    24028.000000
83082001                149868.94                   2015            83082001    146.000000
73202001                30344893.96                 2016            73202001    425610.000000
48081001                8201767.45                  2016            48081001    29.000000
85364103                100958.22                   2017            85364103    341099.000000
84831003                6971766.61                  2017            84831003    3074.000000

and I’m trying to get this goal

Fracción Importación    Valor Comercial Importación 2015        Valor Comercial 2016        Valor Comercial     2017        Valor Comer
85045099                4185787.29                  85045099    307032.000000   73202001     425610.000000      85364103    341099.000000   
84819099                419688356.99                84819099    398684.000000   48081001     29.000000          84831003    3074.000000
84212199                11248672.39                 84212199    24028.000000    null        null                null        null
83082001                149868.94                   83082001    146.000000      null        null                null        null

Following the path:

  1. I created a table where I store the data that I want to pivot
  2. I stored the data with select * into #table from QUERYTHATIUSETOGETINFO
  3. I create the pivot

This is what pivot looks like obviously I don’t want the max value but the pivot just let me run it like this

declare @cols as nvarchar(max), @query as nvarchar(max)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME((Fecha de pago)) 
                from #reportTable
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

set @query = 'select * from #reportTable 
 pivot
 (
    max((Fraccion))
    for (Fecha de pago) in ('+ @cols +')
 ) u
 '
exec sp_executesql @query;

And I get this result but it shows with the max value

And the result is:

select (Fracción Importación), (Valor Comercial Importación), (2015),(2016),(2017),(2018),(2019),(2020),(2021) from #reportTable 
 pivot
 (
    max((Fraccion))
    for (Fecha de pago) in ((2015),(2016),(2017),(2018),(2019),(2020),(2021))
 ) u     

If I use unpivot I get the following error:

Msg 207, Level 16, State 1, Line 114
Invalid column name '2015'.
Msg 207, Level 16, State 1, Line 114
Invalid column name '2016'.
Msg 207, Level 16, State 1, Line 114
Invalid column name '2017'.
.
.
.
The column name "Fraccion" specified in the UNPIVOT operator conflicts with the existing column name in the UNPIVOT argument.

So, after all the context, Is there a way to do this with pivot or unpivot?? or there’s another stuff to implement it?