Skip to Main Content
  • Questions
  • Requirement: Using oracle plsql Case expression or case statement to retrieve date value that falls in below 2 scenario

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Pradeep_Rangan_kr.

Asked: April 08, 2022 - 10:12 pm UTC

Last updated: April 07, 2026 - 6:27 am UTC

Version: Oracle R12.2.3

Viewed 10K+ times! This question is

You Asked

Requirement: Dynamically assign the date value to the main cursor query to fetch data prior to past 3 business days. This is based on the day of the run of the concurrent program

i.e. if the concurrent program is run on SYSDATE i.e. today. IT needs to pick up data prior to last 3 business days and assign the attribution values. Ex as s below

SELECT a.inventory_item_id,
a.segment1,
a.segment2,
a.segment3,
a.organization_id,
a.description,
b.long_description,
a.Inventory_Item_Status_Code,
a.ITEM_CATALOG_GROUP_ID,
c.D_IM_GAAP_LOCK
FROM apps.mtl_system_items_b a,
apps.mtl_system_items_tl b,
APPS.EGO_D_IM_GAAP_LOCK_AGV c
WHERE     a.inventory_item_id = b.inventory_item_id(+)
AND a.organization_id = b.organization_id(+)
AND a.INVENTORY_ITEM_ID = C.INVENTORY_ITEM_ID(+)
AND a.organization_id = C.ORGANIZATION_ID(+)
AND a.segment1 IN (SELECT VALUE
FROM TABLE (
xxego.xxego_pim_common_api.returnastable (
v_sku_list,
',')))
AND a.organization_id = 82
AND a.segment2 = 'SKU'
AND C.D_IM_GAAP_LOCK = 'Y'
AND TRUNC (a.creation_date) BETWEEN
(SELECT   TRUNC (TO_DATE (pin_start_date, 'DD-MM-YYYY'))
- rnum
FROM (SELECT rnum,
ROW_NUMBER () OVER (ORDER BY rnum) rn
FROM (    SELECT LEVEL rnum
FROM DUAL
CONNECT BY LEVEL <= 1500
ORDER BY 1)
WHERE TO_CHAR (
TRUNC (
TO_DATE (pin_start_date,
'DD-MM-YYYY'))
- rnum,
'DY') NOT IN ('SAT', 'SUN'))
WHERE rn = 3)
AND TRUNC (
TO_DATE (pin_end_date,
'DD-MM-YYYY'))
AND EXISTS
(SELECT 1
FROM Apps.ego_catalog_groups_v
WHERE     catalog_group IN ('IM Service',
'Variable Item')
AND item_catalog_group_id IN ('1201', '418210'));



Above query fetches the data for the SYSDATE - 3 Business days period. This would be the default query to be run if it is WD (i.e. weekdays between 2 and 6 (Monday to Friday) ignoring the week end. However if the business logic needs the job to pick up the data for the previous SAT, SUN & Monday (i.e. previous week) if this concurrent program job is run on the friday considering the 3 day business logic, we need to have the program evaluate the date dynamically to be able to pick and attribute the values.

Note: The above query is the main part of the cursor in the CP. Looking forward to your expertise here.

Basic logic i.e. point 1 is already in place as part of the inline subquery. Need expertise on the 2nd point.

1)Cp should have SKU list and start and end date parameters so user/appsupport can run in manually on adhoc basis.
2)Weekend records will be processed after 3 business days.
e.g. 5th(sat) and 6thSsun) March Records will be processed on 11th(Friday) March along with 7th(monday) March records.

and Chris said...

First up, what is considered a working day varies significantly between businesses and countries.

You mention skipping weekends, but what about public holidays? If these are non-working days, how do you handle changes to these (e.g. moving/adding holidays)?

To simplify non-working logic I strongly recommend creating a dates table with a row per day. Have a flag to state whether or not it's a working day:

create table calendar_dates (
  calendar_day   date not null primary key,
  is_working_day varchar2(1) not null,
  check ( calendar_day = trunc ( calendar_day ) )
);


Then you can find the last three working days with a query like:

where table_date in (
  select calendar_day from calendar_dates
  where  calendar_day = trunc (sysdate)
  and    is_working_day = 'Y'
  order  by calendar_day desc
  fetch  first 3 rows only
);


If you need to find all the dates - including non-working days - in the range, you can do something like this:

with working_days as (
  select *
  from   calendar_dates
  where  calendar_day = trunc (sysdate)
  and    is_working_day = 'Y'
  order  by calendar_day desc
  fetch  first 3 rows only
), working_date_range as (
  select min ( calendar_day ) start_date, 
         min ( calendar_day ) end_date
  from   working_days 
)
  select * from your_table yt
  join   working_date_range wdr
  on     yt.table_date between wdr.start_date and wdr.end_date;


I discuss the topics of bypassing non-working days in detail in this post:

https://blogs.oracle.com/sql/post/how-to-find-the-next-business-day-and-add-or-subtract-n-working-days-with-sql

It includes solutions where you don't have a specific dates table.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Query resolved

Pradeep Rangan KR, April 03, 2026 - 2:32 pm UTC

Hi Chris, Apologize for the delay in responding back!! Thanks a ton for your feedback, At that time we did rewrite the query logic using the calendar table format (custom to our org) and passed the weekdays (working days) / excluding the weekend ( sat/sun). The query has been rewritten as dynamic query. Appreciate all the help and assistance you provided through this post.

Thanks,
Pradeep KR
Connor McDonald
April 07, 2026 - 6:27 am UTC

Glad we could help

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.