简体   繁体   中英

Oracle: select week of the year according to an input list

I had a two tables who looks like:

Table A:

  ORG_DATE
03/04/2018
02/10/2019
17/12/2018

Table B:

Start_date   End_Date     Week_of_Yr
01/04/2018  08/04/2018    2018041
01/10/2019  08/10/2019    2019101
14/12/2018  21/12/2018    2018123

The goal is to achieve a table who looks like:

  ORG_DATE      Week_of_Yr
03/04/2018       2018041
02/10/2019       2019101
17/12/2018       2018123

Simply assign the correct week of the year when the field ORG_DATE from table A, falls between start_date and end_date of table B.

So far i tried the following because there aren't fields for making a possible join between the tables:

SELECT 
Week_of_Yr
FROM TABLE B
WHERE TO_DATE((SELECT 
               TO_CHAR(A11.ORG_DATE, 'DD/MM/YYYY') ORG_DATE
               FROM TABLE_A A11
               GROUP BY A11.ORG_DATE), 'DD/MM/YYYY') BETWEEN Start_date AND End_Date;

However, the query doesn't accept a list as an input. Any ideas ?

Thanks

Just join them checking the org_date falls in between the start_date and end_date in the ON clause.

SELECT a.org_date,
       b.week_of_yr
       FROM a
            LEFT JOIN b
                      ON b.start_date <= a.org_date
                         AND b.end_date > a.org_date;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM