    SELECT
        amor.ID_BV,
        amor.DESCRIPCION as ESTUDIO,
        amor.TE as T_E,
        amor.NIVEL AS NIVEL,
        amor.ANIO_0 as AMOR_ANIO_1,
        visual.ANIO_0 as VIS_ANIO_1,
        DECODE( visual.ANIO_0, 0, 0, ROUND( amor.ANIO_0 / visual.ANIO_0, 2 ) ) as CPV_ANIO_1,
        amor.ANIO_1 as AMOR_ANIO_2,
        visual.ANIO_1 as VIS_ANIO_2,
        DECODE( visual.ANIO_1, 0, 0, ROUND( amor.ANIO_1 / visual.ANIO_1, 2 ) ) as CPV_ANIO_2,
        amor.ANIO_2 as AMOR_ANIO_3,
        visual.ANIO_2 as VIS_ANIO_3,
        DECODE( visual.ANIO_2, 0, 0, ROUND( amor.ANIO_2 / visual.ANIO_2, 2 ) ) as CPV_ANIO_3,
        amor.ANIO_3 as AMOR_ANIO_4,
        visual.ANIO_3 as VIS_ANIO_4,
        DECODE( visual.ANIO_3, 0, 0, ROUND( amor.ANIO_3 / visual.ANIO_3, 2 ) ) as CPV_ANIO_4,
        amor.ANIO_4 as AMOR_ANIO_5,
        visual.ANIO_4 as VIS_ANIO_5,
        DECODE( visual.ANIO_4, 0, 0, ROUND( amor.ANIO_4 / visual.ANIO_4, 2 ) ) as CPV_ANIO_5
    FROM
      (
        --SELECT * FROM (
           SELECT
                CASE
                   WHEN DESC_ESTUDIO IS NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL THEN
                  ''
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL THEN
                  'Estudio'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL THEN
                  'Titulo'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND ID_BV IS NOT NULL THEN
                  ID_BV 
                END as ID_BV,
               DESC_ESTUDIO,
                NOMBRE_UNICO,
                DESC_TITULO,	 
 TEMPORADA, EPISODIO,
                CASE 
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND NOMBRE_UNICO = DESC_TITULO THEN
                  1
                   ELSE
                  0              
                END as NOT_SERIE_SINGLE,
                CASE
                   WHEN DESC_ESTUDIO IS NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL THEN
                  0
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL THEN
                  1
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL THEN
                  2
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL THEN
                  3              
                END as NIVEL,
                CASE 
                   WHEN DESC_ESTUDIO IS NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  1
                   ELSE
                  0               
                END as ORDEN_N1,
                CASE 
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  1
                   ELSE
                  0               
                END as ORDEN_N2,
                CASE 
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NULL THEN
                  1
                   ELSE
                  0               
                END as ORDEN_N3,
                CASE 
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL THEN
                  1
                   ELSE
                  0               
                END as ORDEN_N4,
                CASE
                   WHEN DESC_ESTUDIO IS NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  'TOTAL'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  DESC_ESTUDIO
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO || ' Temporada '|| temporada
				   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NOT NULL THEN
                  NOMBRE_UNICO || ' Temporada Unica'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND ID_BV IS NOT NULL THEN
                  DESC_TITULO              
                END as DESCRIPCION,	
CASE
                  WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND ID_BV IS NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NULL THEN
                  'Temporada '|| TEMPORADA
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND ID_BV IS NOT NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NOT NULL THEN
                  EPISODIO
                END as TE,
                CASE
                   WHEN DESC_ESTUDIO IS NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  'TOTAL'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  DESC_ESTUDIO
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO
				   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO || ' Temporada '|| temporada
				   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NOT NULL THEN
                  NOMBRE_UNICO || ' Temporada Unica'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND ID_BV IS NOT NULL THEN
                   DESC_ESTUDIO || ' ' || DESC_TITULO || ' ' || ID_BV
                END as JOINING,
                ROUND( SUM( ANIO_0 ), 2 ) as ANIO_0, 
                ROUND( SUM( ANIO_1 ), 2 ) as ANIO_1, 
                ROUND( SUM( ANIO_2 ), 2 ) as ANIO_2, 
                ROUND( SUM( ANIO_3 ), 2 ) as ANIO_3, 
                ROUND( SUM( ANIO_4 ), 2 ) as ANIO_4
           FROM 
                (
                        SELECT
                            DESC_ESTUDIO, DESC_TITULO, NOMBRE_UNICO, ID_BV, temporada, episodio,
                            NVL( ANIO_0, 0 ) as ANIO_0, NVL( ANIO_1, 0 ) as ANIO_1, 
                            NVL( ANIO_2, 0 ) as ANIO_2, NVL( ANIO_3, 0 ) as ANIO_3, 
                            NVL( ANIO_4, 0 ) as ANIO_4
                        FROM
                        (
                            SELECT       
                                est.DESC_ESTUDIO,
                                amor.ID_TITULO_CNTORIG, 
                                ct.ID_BV,
                                ct.DESC_TITULO,
                                ct.NOMBRE_UNICO,
                                ct.TEMPORADA,
                                    ct.EPISODIO,
                                TO_NUMBER( TO_CHAR( amor.MES, 'yyyy' ) ) as ANNIO,
                                TO_NUMBER( TO_CHAR( amor.MES, 'mm' ) ) as MESES, 
                                TO_NUMBER( amor.MONTO ) as MONTO
                            FROM
                                DDA_T_AMORTIZACIONCPV amor
                                INNER JOIN ( 
                                    SELECT 
                                    scnt.ID_CONTRATO_ORIGINAL AS ID_CONTRATO,
                                    ct.ID_TITULO_CNTORIG,
                                    ct.ID_BV,
                                    ct.DESC_TITULO,
                                    ct.NOMBRE_UNICO,
                                    ct.TEMPORADA,
                                    ct.EPISODIO,
                                    ct.FECHA_INICIO,
                                    ct.FECHA_FIN,
                                    ct.COSTO
                                    FROM DDA_T_CONTRATO_TITULO ct,
                                         (SELECT ID_CONTRATO_ORIGINAL,
                                          MAX( ID_CONTRATO ) AS ID_CONTRATO
                                          FROM DDA_T_CONTRATO
                                          WHERE ID_TIPO_CONTRATO = 1
                                          GROUP BY ID_CONTRATO_ORIGINAL) scnt
                                    WHERE ct.ID_CONTRATO = scnt.ID_CONTRATO) 
                                ct on ct.ID_TITULO_CNTORIG = amor.ID_TITULO_CNTORIG
                                INNER JOIN  DDA_T_CONTRATO cnt on cnt.ID_CONTRATO = amor.ID_CONTRATO_ORIGINAL  
                                      AND  cnt.ID_CONTRATO = ct.ID_CONTRATO
                                INNER JOIN DDA_C_ESTUDIO est on est.ID_ESTUDIO = cnt.ID_ESTUDIO 
                            WHERE                                 
                                TO_DATE( TO_CHAR( amor.MES, 'mm-yyyy' ), 'mm-yyyy' ) <= TO_DATE(#{mes} || '-' || #{anio_final}, 'mm-yyyy' )
                                AND TO_NUMBER( TO_CHAR( amor.MES, 'yyyy' ) ) >= #{anio_inicial}
                                AND cnt.ID_ESTATUS in ( 4, 5, 6 )
                        )
                        PIVOT( SUM( MONTO ) FOR 
                            ANNIO in ( #{pivot_data}  )
                            )                            
           )GROUP BY
                GROUPING SETS
                    ( 
                        ( ),
                        (DESC_ESTUDIO),
                        (DESC_ESTUDIO, NOMBRE_UNICO, TEMPORADA ),
                        (DESC_ESTUDIO, NOMBRE_UNICO, DESC_TITULO,  TEMPORADA, EPISODIO, ID_BV )
                    ) 
        --)WHERE NOT_SERIE_SINGLE != 1 
      ) amor,
      (
       -- SELECT * FROM (
           SELECT 
                --CASE 
                --   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND NOMBRE_UNICO = DESC_TITULO THEN
                --  1
                --   ELSE
                --  0              
                --END as NOT_SERIE_SINGLE,
                CASE
                   WHEN DESC_ESTUDIO IS NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  'TOTAL'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  DESC_ESTUDIO
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO || ' Temporada '|| temporada
				   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NOT NULL THEN
                  NOMBRE_UNICO || ' Temporada Unica'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND ID_BV IS NOT NULL THEN
                  DESC_TITULO               
                END as DESCRIPCION,
                TEMPORADA, EPISODIO,	  
CASE
                  WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND ID_BV IS NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NULL THEN
                  'Temporada '|| TEMPORADA
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND ID_BV IS NOT NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NOT NULL THEN
                  EPISODIO
                END as TE,
                CASE
                   WHEN DESC_ESTUDIO IS NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  'TOTAL'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  DESC_ESTUDIO
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO
				   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NOT NULL AND EPISODIO IS NULL THEN
                  NOMBRE_UNICO || ' Temporada '|| temporada
				   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NULL AND TEMPORADA IS NULL AND EPISODIO IS NOT NULL THEN
                  NOMBRE_UNICO || ' Temporada Unica'
                   WHEN DESC_ESTUDIO IS NOT NULL AND NOMBRE_UNICO IS NOT NULL AND DESC_TITULO IS NOT NULL AND ID_BV IS NOT NULL THEN
                   DESC_ESTUDIO || ' ' || DESC_TITULO || ' ' || ID_BV
                END as JOINING,
                SUM( ANIO_0 ) as ANIO_0, SUM( ANIO_1 ) as ANIO_1, SUM( ANIO_2 ) as ANIO_2, SUM( ANIO_3 ) as ANIO_3, SUM( ANIO_4 ) as ANIO_4
           FROM 
            (
                SELECT
                    DESC_ESTUDIO, 
                    DESC_TITULO,
                    NOMBRE_UNICO, ID_BV,  TEMPORADA, EPISODIO,
                    NVL( ANIO_0, 0 ) as ANIO_0, NVL( ANIO_1, 0 ) as ANIO_1, 
                    NVL( ANIO_2, 0 ) as ANIO_2, NVL( ANIO_3, 0 ) as ANIO_3, 
                    NVL( ANIO_4, 0 ) as ANIO_4
                FROM
                (
                    SELECT
                        e.DESC_ESTUDIO, vc.ID_BV,
                        vc.DESC_TITULO, vc.NOMBRE_UNICO,  vc.TEMPORADA, vc.EPISODIO,
                        TO_NUMBER( TO_CHAR( vc.MES, 'yyyy' ) ) as ANNIO,
                        TO_NUMBER( TO_CHAR( vc.MES, 'mm' ) ) as MESES, 
                        SUM( vc.CANTIDAD ) as TOTAL
                    FROM
                        (  
                          WITH base_query AS (      
                             SELECT  ct.ID_LISTA, ct.ID_BV, ct.ID_ESTUDIO, v.MES, v.CANTIDAD, ct.NOMBRE_UNICO, ct.DESC_TITULO, ct.TEMPORADA, ct.EPISODIO
                             FROM  DDA_T_VISUALIZACION v
                               INNER JOIN(   
                                 SELECT  t.ID_LISTA, t.ID_BV, c.ID_ESTUDIO, t.FECHA_INICIO, t.FECHA_FIN, t.NOMBRE_UNICO, t.DESC_TITULO,
								 t.TEMPORADA, t.EPISODIO
                                 FROM DDA_T_CONTRATO_TITULO t
                                   INNER JOIN DDA_T_CONTRATO c on c.ID_CONTRATO = t.ID_CONTRATO
                                 WHERE c.ID_ESTATUS in ( 4, 5, 6 )
                               )ct ON ct.ID_BV = v.ID_BV
                             WHERE v.MES BETWEEN ADD_MONTHS((LAST_DAY(ct.FECHA_INICIO)+1),-1) AND LAST_DAY(ct.FECHA_FIN)
                                  AND TO_DATE( TO_CHAR( v.MES, 'mm-yyyy' ), 'mm-yyyy' ) <= TO_DATE(#{mes} || '-' ||#{anio_final}, 'mm-yyyy' )                 
                                  AND TO_NUMBER( TO_CHAR( v.MES, 'yyyy' ) ) = #{anio_final}
                          )
                           SELECT DISTINCT a.ID_BV, a.ID_ESTUDIO, a.MES, a.CANTIDAD, a.NOMBRE_UNICO, a.DESC_TITULO, a.TEMPORADA, a.EPISODIO 
                           FROM base_query a
                             INNER JOIN (
                               SELECT ID_BV, ID_ESTUDIO, MES, MAX(ID_LISTA) AS ID_LISTA  FROM base_query 
                               GROUP BY ID_BV, ID_ESTUDIO, MES
                           ) b ON a.ID_BV = b.ID_BV AND a.MES = b.MES AND a.ID_ESTUDIO = b.ID_ESTUDIO AND a.ID_LISTA = b.ID_LISTA 
                        ) vc
                        INNER JOIN DDA_C_ESTUDIO e ON e.ID_ESTUDIO = vc.ID_ESTUDIO 
                    GROUP BY e.DESC_ESTUDIO, vc.MES, vc.DESC_TITULO, vc.NOMBRE_UNICO, vc.ID_BV , vc.TEMPORADA, vc.EPISODIO--, vc.ES_SERIE
                )
                PIVOT( SUM( TOTAL ) FOR 
                    ANNIO in ( #{pivot_data} )
                    )   
            )GROUP BY
                GROUPING SETS
                (
                      ( ),
                      (DESC_ESTUDIO),
                      (DESC_ESTUDIO, NOMBRE_UNICO, TEMPORADA ),
                      (DESC_ESTUDIO, NOMBRE_UNICO, DESC_TITULO,  TEMPORADA, EPISODIO, ID_BV )
                )
       -- ) WHERE NOT_SERIE_SINGLE != 1
      ) visual
    WHERE
      amor.JOINING = visual.JOINING(+)
    ORDER BY ORDEN_N1, DESC_ESTUDIO, ORDEN_N2 DESC, NOMBRE_UNICO, decode(is_number(amor.TEMPORADA),0,0,to_number(amor.TEMPORADA)), ORDEN_N3 DESC,decode(is_number(amor.episodio),0,0,to_number(amor.episodio)), DESC_TITULO DESC, ORDEN_N4 DESC 
