SQL - Query Mes actual, mes anterior y trimestre pasado por país y motor

 
Vista:

Query Mes actual, mes anterior y trimestre pasado por país y motor

Publicado por Cristina (1 intervención) el 23/08/2021 10:02:31
Buenos días,

Tengo esta lista de fechas, motores y países (a futuro vendrán más registros para cada campo):

data_date_part engine country_code
2021-06-30 dividend ES
2021-05-31 ir_delta
2021-04-30 repomargin
2021-04-05
2021-03-31
2021-03-15
2020-06-30
2020-05-31

Que corresponden a la siguiente tabla, porque cada engine tiene una fecha distinta:

country_code data_date_part engine
ES 2021-06-30 dividend
ES 2021-05-31 dividend
ES 2021-04-30 dividend
ES 2021-04-05 dividend
ES 2021-03-31 dividend
ES 2021-03-15 dividend
ES 2021-04-30 ir_delta
ES 2021-03-15 ir_delta
ES 2021-06-30 repomargin
ES 2021-05-31 repomargin
ES 2021-03-31 repomargin
ES 2020-06-30 repomargin
ES 2020-05-31 repomargin


Lo que necesito conseguir, es traer para cada engine (dividend, ir_delta, repomargin) y cada país, 3 fechas.
1. La fecha máxima (la más actual hasta el momento)
2. La fecha correspondiente a fin de mes, del mes anterior al máximo obtenido.
3. La fecha de cierre de trimestre, respecto a esa fecha máxima inicial.

Siguiendo un ejemplo, para dividend, sería:
1. Fecha máxima sería el 2021-06-30
2. Fecha de cierre de mes anterior sería 2021-05-31
3. El cierre de trimestre sería 2021-03-31. Si la fecha del 2021-03-31 no existiese, la fecha de cierre de trimestre debería ser 2021-03-15, porque sería la máxima existente de marzo y la tendría que tomar como un cierre.

Ahora mismo la query que tengo hecha es la siguiente:

select *,
tab1.data_date_part,
tab1.country_code,
tab1.engine
from bu_fva.fva_output_engines tab1
INNER JOIN (select country_code, engine, data_date_part,
row_number() over(partition by country_code, engine, left(data_date_part,7)
order by country_code, engine, data_date_part desc) as fila
from bu_fva.fva_output_engines) tab2
ON tab1.country_code=tab2.country_code and tab1.engine=tab2.engine and tab1.data_date_part=tab2.data_date_part
where fila < 3;

y me da como resultado:

Country Code data_date_part Engine
ES 2021-06-30 dividend
ES 2021-05-31 dividend
ES 2021-04-30 dividend
ES 2021-03-31 dividend
ES 2021-04-30 ir_delta
ES 2021-03-15 ir_delta
ES 2021-06-30 repomargin
ES 2021-05-31 repomargin
ES 2021-03-31 repomargin
ES 2020-06-30 repomargin
ES 2020-05-31 repomargin


Las marcadas en negrita no debería de traerlas. Además el trimestre no está contemplado en la query, y no se como añadirlo.
Alguien puede ofrecerme su ayuda?
Muchas gracias de antemano
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Query Mes actual, mes anterior y trimestre pasado por país y motor

Publicado por Horroroso (1 intervención) el 25/08/2021 18:50:51
Hola, para ayudarte hice tres views:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FechaMaxima
SELECT fva_output_engines.country_code, fva_output_engines.engine, Max(fva_output_engines.data_date_part) AS MAX_DATE
FROM fva_output_engines
GROUP BY fva_output_engines.country_code, fva_output_engines.engine;
 
 
FechaUltimoCierre
SELECT TAB2.country_code, TAB2.engine, Max(TAB2.data_date_part) AS MaxOfdata_date_part
FROM (SELECT TAB1.country_code, TAB1.engine, TAB1.data_date_part, FechaMaxima.MAX_DATE
FROM fva_output_engines AS TAB1 INNER JOIN FechaMaxima ON (TAB1.engine = FechaMaxima.engine) AND (TAB1.country_code = FechaMaxima.country_code)
WHERE  TAB1.data_date_part<FechaMaxima.MAX_DATE)  AS TAB2
GROUP BY TAB2.country_code, TAB2.engine;
 
FechaUltimoDiaMesAnterior
SELECT FechaMaxima.country_code, FechaMaxima.engine, DateAdd(d,-1,DATEFROMPARTS(Year(MAX_DATE),Month(MAX_DATE),'1')) AS LAST_END_DAY
FROM FechaMaxima;

Espero que te sirva,

Saludos!

-Horroroso-
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar