Thursday, April 9, 2009

Going back 12 months based on selected month

If users need capability to get back rolling 12 months of data based on one month selected in dashboard, the following code will do this:

"Month"."Month" between (CASE WHEN SUBSTRING('@{AsOfMonth}' FROM 8 FOR 2) = '12' THEN SUBSTRING('@{AsOfMonth}' FROM 1 FOR 4) ' / 01' ELSE SUBSTRING((CAST((CAST((SUBSTRING('@{AsOfMonth}' FROM 1 FOR 4) SUBSTRING ('@{AsOfMonth}' FROM 8 FOR 2)) AS INTEGER) - 99 ) AS CHAR(6))) FROM 1 FOR 4) ' / ' SUBSTRING((CAST((CAST((SUBSTRING('@{AsOfMonth}' FROM 1 FOR 4) SUBSTRING ('@{AsOfMonth}' FROM 8 FOR 2)) AS INTEGER) - 99 ) AS CHAR(6))) FROM 5 FOR 2) END) AND '@{AsOfMonth}'

This works if the month in database is in the format 'YYYY / MM'. I had earlier posted a way to do rolling 12 months based on date field in prompts and use TIMESTAMPDIFF function also. However that method has a disagvantage that it does not work with constrained prompts unless the date in database is exactly in the format of 'DD-MMM-YYYY'.

4 comments:

  1. thanks for the post, it did solve my problem although the SQL that you have written is not for Oracle I hope.

    ReplyDelete
  2. The SQL above is Oracle BI syntax, not for the Oracle database itself, if that was your question.

    ReplyDelete
  3. CAn you please provide the correct sysntax.I am getting View Display Error.

    Thanks,
    Suresh.

    ReplyDelete
    Replies
    1. What is the detailed error under view display error? Did you populate the variable @{AsOfMonth} in the dashboard properly?

      Delete