0

hi i have a goals want to generate list of date between 2 date but i also have some condition like this :

Table_A : Maximum Delivery Time and Week off

Code      Max_delivery_time      Week_off
 01               4                  6

Note : Maximum_delivery_time it contains the number of day that we can deliver an item week_off it contains the day in every week that deliver can't be process values 1-7 1 = monday, 2 = tuesday etc

Table_B : Table Day off

Code       Date_day_off 
 01         2021-26-05
 02         2021-28-05

Table B contains date day off that must be exclude if the day is on range

so i would like to select list of the date inside the date range with condition : date start must be +1 day from today date finish is (sysdate+1) + "Max_delivery_time". But if the day inside is contains a day in Week_off and date from table b date_day_off it will change to next day sample : Today is : monday, 2021-05-24

the result will be

List_of_day
2021-05-25
2021-05-27
2021-05-30
2021-05-31

is it possible to generate it with a query ?

6
  • what is the type of Date_day_off? is it really year day month? Commented May 24, 2021 at 13:36
  • 2 questions: 1) your sample output just has the four dates for your sample Table_A record, but uses all the dates from Table_B, even the one with a different Code; does Code in Table_A not have anything to do with Code in Table_B? 2) what does the output look like if there are multiple rows in Table_A? Commented May 24, 2021 at 13:40
  • what does select version(); show? Commented May 24, 2021 at 13:40
  • Table_A - does it contains 1 row only? 1 = monday, 2 = tuesday etc Why so strange? Why not like for WEEKDAY() function (0 = Monday, 1 = Tuesday, … 6 = Sunday). Table_B - incorrect date literals. the result will be Why '2021-05-31' is skipped? Commented May 24, 2021 at 13:53
  • Welcome to SO. Please see: Why should I provide an MCRE for what seems to me to be a very simple SQL query? Commented May 24, 2021 at 13:59

1 Answer 1

0

one way is to use recursive cte :

with recursive cte as (
    select 
        CURRENT_DATE() List_of_day 
        , Max_delivery_time 
        , Week_off
        , 0  as counter 
        , DAYOFWEEK(CURRENT_DATE()) - 1 DAYno
        , 1 as flag 
    from Table_A ta 
    union all 
    select ADDDATE(List_of_day, 1 ) List_of_day 
        , Max_delivery_time + case when DAYOFWEEK(ADDDATE(List_of_day, 1 )) -1 = Week_off then 1 when b.code is not null then 1  else 0 end  Max_delivery_time 
        , Week_off
        , counter +1 counter
        , DAYOFWEEK(ADDDATE(List_of_day, 1 )) - 1 DAYno
        , case when DAYOFWEEK(ADDDATE(List_of_day, 1 )) - 1 = Week_off then 1 when b.code is not null then 1  else 0 end as flag 
    from cte 
    left join Table_B b on  ADDDATE(List_of_day, 1 ) = b.Date_day_off
    where counter +1 <= Max_delivery_time
)
select List_of_day from cte
where flag = 0 

db<>fiddle here

Sign up to request clarification or add additional context in comments.

2 Comments

this is work in my sql 8.0 and it's correct what i means. but could i use recursive in mysql under 8.0 ? i think it doesn't work :(
@Deaaa mysql as of February 1, 2021 mysql 5.6 is not even supported anymore , I recommend upgrade it to latest version MySQL Product Support EOL Announcements

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.