Forum Stats

  • 3,826,847 Users
  • 2,260,713 Discussions


Oracle Apex Modfiy Grid query to fetch previous quarter data if current not available

Vinipanda Member Posts: 106 Red Ribbon

I have an apex application that shows department wise project data for every quarter.

So on home page we select department, quarter and year and then for the same data is shown in all pages.

For one page that shows KPI data in interactive grid ,I need to modify my query as per logic required.

It needs to fetch data quarterly, however if say data is not available for a particular quarter it'd take the data from previous quarter. 

So, if for Quarter 1,2021 data is not available, it should fetch from Quarter 4,2020.

If Quarter 2,2021 is not available, it should fetch Quarter 1,2021 data.

Tables with data for clarity:

The first tale is DEPT_QUARTER_DATA which stores id for each quarter of department.

So, each dept_id for each quarter has a particular quarter_pk.

Also 10 is First quarter, 11 is Second quarter,12 is Third quarter and 13 is fourth quarter.

DEPT_DATA table which has main data:

It has sample data for 2 departments.

So, ìn first row,dept_quarter_id =1 indicates Quarter 1,2020 data, for dept_id=1.



Now the query should be, say there is no data for dept_id=1 in dept_quarter_id=2, then it should fetch data of dept_quarter_id=1.i.e. go backwards.

If there is no data for dept_quarter_id=5, i.e.Quarter 1,2021, it should fetch data from dept_quarter_id=4, i.e. Quarter 4,2020.

Current Query is and i understand it needs to be modified:

      ,case when KPI_1 is null then (Select KPI_1 from DEPT_DATA where DEPT_QUARTER_ID
  in(select quarter_pk from DEPT_QUARTER_DATA where DEPT_ID=:DEPT_ID and
  quarter = :QUARTER-1 and year = YEAR and QUARTER_PK= :DEPT_QUARTER_ID)and PROJECT = :P1_PROJECT) else KPI_1 end as KPI_1
  from DEPT_DATA

QUARTER, PROJECT,YEAR are all global items being passed based on DEPT_QUARTER_ID of the project which has been set as application item.

Apex: 20.2



  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 42,067 Red Diamond

    Hi, @Vinipanda

    Whenever you have a question, please post a little sample data in a usable form (such as CREATE TABLE and INSERT statements), so the people who want to help you car re-create the problem and test their ideas. Also post the exact results you want from the given data, and an explanation (in general terms) of you get those results from that data.

    Always say which version of Oracle you're using (e.g.

    So, if the quarter you want isn't available, then you want the quarter before that. That sounds like a job for a Top-N Query. In a sub-query, fetch data from the quarter you want and all previous quarters (or just the one previous, depending on your requirements), and use the analytic DENSE_RANK function to number them 1, 2, 3, .. with 1 being the latest one actually present. Then, in the super-query, include only the rows numbered 1.

  • Frank Kulash
    Frank Kulash Member, Moderator Posts: 42,067 Red Diamond


    Since you're always displaying only one quarter, you could also start by finding out which is the last quarter actually present on or before the quarter you want, and then including only that quarter.

  • Abdulkareem Al-Badani
    Abdulkareem Al-Badani Member Posts: 29 Blue Ribbon

    @Vinipanda a better query for your scenario is:

    select DEPT_ID,DEPT_QUARTER_ID,nvl(KPI_1 ,previous_QUARTER_data) as KPI_1

     from (


         lag(KPI_1) over (order by DEPT_QUARTER_ID) as previous_QUARTER_data

     from DEPT_DATA