-1

Pull the data month end and weekend without weekends.if get the weekend then retrieve the before that weekend. Ex:the date is 20 or 21 Of AUGUST(WEEKENDS),Then i need the 19th(WEEKDAY) data..

1 Answers1

0

O r a c l e
You could use CASE expression with To_Char(Date_column, 'd').
The WITH clause is here just to generate some sample data and, as such, it is not a part of the answer.
Here is the answer including the order number of the day of the week ('d') and short name of the day of the week ('DY') because the day one of the week depends on NLS_TERRITORY.
Here is the code...

 WITH
    tbl AS
        (
            Select SYSDATE - (LEVEL - 1) "A_DATE", 101 - LEVEL "A_VALUE" From Dual CONNECT BY LEVEL <= 18
        )
SELECT
    A_DATE "A_DATE",
    To_Char(A_DATE, 'd') "A_DAY_OF_WEEK",
    To_Char(A_DATE, 'DY') "A_DAY_OF_WEEK_NAME",
    A_VALUE "TBL_VALUE",
    CASE  WHEN To_Char(A_DATE, 'd') =  '6' THEN
             (Select A_VALUE From tbl Where A_DATE = t1.A_DATE - 1)
          WHEN To_Char(A_DATE, 'd') =  '7' THEN
              (Select A_VALUE From tbl Where A_DATE = t1.A_DATE - 2)
    ELSE
        A_VALUE
    END "VALUE_OUT"
FROM
    tbl t1
ORDER BY A_DATE

... and the result

--  
--  R e s u l t
--  
--  A_DATE    A_DAY_OF_WEEK A_DAY_OF_WEEK_NAME  TBL_VALUE  VALUE_OUT
--  --------- ------------- ------------------ ---------- ----------
--  03-AUG-22 3             WED                        83         83 
--  04-AUG-22 4             THU                        84         84 
--  05-AUG-22 5             FRI                        85         85 
--  06-AUG-22 6             SAT                        86         85 
--  07-AUG-22 7             SUN                        87         85 
--  08-AUG-22 1             MON                        88         88 
--  09-AUG-22 2             TUE                        89         89 
--  10-AUG-22 3             WED                        90         90 
--  11-AUG-22 4             THU                        91         91 
--  12-AUG-22 5             FRI                        92         92 
--  13-AUG-22 6             SAT                        93         92 
--  14-AUG-22 7             SUN                        94         92 
--  15-AUG-22 1             MON                        95         95 
--  16-AUG-22 2             TUE                        96         96 
--  17-AUG-22 3             WED                        97         97 
--  18-AUG-22 4             THU                        98         98 
--  19-AUG-22 5             FRI                        99         99 
--  20-AUG-22 6             SAT                       100         99
d r
  • 3,848
  • 2
  • 4
  • 15