question

OraODI avatar image
OraODI asked

Rolling 24 Months

I have requirement of rolling 24 months which is calculated based on below which gives incorrect month when I take for 25th nov 2018 .

Below is the code

case when MONTHS_BETWEEN(to_date(FISCAL_PERIOD_MM,'YYYYMM'),to_date(to_char(add_months(SYSDATE,-1),'YYYYMM'),'YYYYMM'))+ 24  <= 0
  then - MONTHS_BETWEEN(to_date(to_char(last_day(sysdate),'YYYYMM'),'YYYYMM'),to_date(FISCAL_PERIOD_MM,'YYYYMM'))  
  else MONTHS_BETWEEN(to_date(FISCAL_PERIOD_MM,'YYYYMM'),to_date(to_char(add_months(sysdate,-1),'YYYYMM'),'YYYYMM'))+ 24 end FISCAL_MNTH_ENUM,

when I take date as 25th nov 2018 tit needs to calculate from 201811 to 201610 i.e. 24 months from todays date while its giving 201810 and

FISCAL_MNTH_ENUM,also its appending as 23 though it has to be 24 and back trace to 23 ,22,21 and so on than it should take in as -1 ,-2,-3....and so on

Snapshot

201807 20
201808 21
201809 22
201810 23

Please let me know the possible solution for it full code is below

Select dim001_id,
dim007_bill_to_id,
dim012_id,
fisc_mnth.dim003_id,
FISCAL_PERIOD_MM,
case when MONTHS_BETWEEN(to_date(FISCAL_PERIOD_MM,'YYYYMM'),to_date(to_char(add_months(SYSDATE,-1),'YYYYMM'),'YYYYMM'))+ 24 <= 0
then - MONTHS_BETWEEN(to_date(to_char(last_day(sysdate),'YYYYMM'),'YYYYMM'),to_date(FISCAL_PERIOD_MM,'YYYYMM'))
else MONTHS_BETWEEN(to_date(FISCAL_PERIOD_MM,'YYYYMM'),to_date(to_char(add_months(sysdate,-1),'YYYYMM'),'YYYYMM'))+ 24 end FISCAL_MNTH_ENUM,
round(nvl(invline_net_value_usd_lce,0),6) invline_net_value_usd_lce,
round(SUM(nvl(invline_net_value_usd_lce,0)) OVER (PARTITION BY dim001_id, dim007_bill_to_id, dim012_id ORDER BY fiscal_period_mm ROWS BETWEEN 23 PRECEDING AND CURRENT ROW),6) AS invline_net_value_usd_lce_24m,
round(nvl(invline_net_value_eur_lce,0),6) invline_net_value_eur_lce,
round(SUM( nvl(invline_net_value_eur_lce,0)) OVER (PARTITION BY dim001_id, dim007_bill_to_id, dim012_id ORDER BY fiscal_period_mm ROWS BETWEEN 23 PRECEDING AND CURRENT ROW),6) AS invline_net_value_eur_lce_24m
from
(Select fct001.dim001_id,
fct001.dim007_bill_to_id,
fct001.dim012_id,
fct001.dim003_id,
SUM(fct001.invoice_line_net_value_usd_lce ) AS invline_net_value_usd_lce,
SUM(fct001.invoice_line_net_value_usd_lce* cf.FACTOR_EUR ) AS invline_net_value_eur_lce
from
FIDM.fct001_invoice fct001,
FIDM.dim007_customer dim007,
FIDM.dim003_fiscal_time dim003,
(select FISCAL_PERIOD_MM,(1/FACTOR) AS FACTOR_EUR from FIDM.LKP001_CURRENCY_FACTOR WHERE CURRENCY_FROM='EUR' AND CURRENCY_TO='USD' AND FACTOR_TYPE='H2') cf
where
fct001.dim007_bill_to_id = dim007.dim007_id and
dim007.INTER_COMPANY <> 'Y' and
fct001.dim003_id =dim003.dim003_id and
dim003.fiscal_period_mm = cf.FISCAL_PERIOD_MM and dim001_id =1237 and dim007.dim007_id =1796978 and fct001.dim012_id =10
group by
fct001.dim001_id,
fct001.dim007_bill_to_id,
fct001.dim012_id,
fct001.dim003_id) cust_data
PARTITION BY (cust_data.dim001_id,cust_data.dim007_bill_to_id,cust_data.dim012_id)
right outer join
(SELECT dim003_id, fiscal_period_mm ,from_date,TO_DATE FROM FIDM.dim003_fiscal_time WHERE dim003_id >= (select min(dim003_id) from FIDM.fct001_invoice)
and from_date < ADD_MONTHS(sysdate, -1)) fisc_mnth
on
cust_data.dim003_id = fisc_mnth.dim003_id
--order by 1,2,3,4
;

oracle
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers

·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.