最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sql - Find orders in the same time frame in Oracle - Stack Overflow

programmeradmin0浏览0评论

here is the date as an example

 select  'part1' as part , 'supplierAAA' as supplier , 'order1' as order_name, 'E' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('31-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part2' as part , 'supplierBBB' as supplier , 'order2' as order_name, 'E' as order_type , to_date('01-01-2022','dd-mm-yyyy') as start_date , to_date('31-12-2022','dd-mm-yyyy') as end_date from dual
union all 
 select  'part2' as part , 'supplierBBB' as supplier , 'order4' as order_name, 'G' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('31-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order5' as order_name, 'E' as order_type , to_date('01-01-2022','dd-mm-yyyy') as start_date , to_date('30-06-2022','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order6' as order_name, 'E' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('31-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order7' as order_name, 'G' as order_type , to_date('01-01-2022','dd-mm-yyyy') as start_date , to_date('30-06-2022','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order8' as order_name, 'G' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('15-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order9' as order_name, 'G' as order_type , to_date('16-12-2023','dd-mm-yyyy') as start_date , to_date('31-12-2024','dd-mm-yyyy') as end_date from dual

there are 2 types of order, E and G. I need to flag all orders which have for the same part and supplier and time frame a G order(s) covering an E order completely.

Because there are Millions of records I don't want to use self join and join the same data set which could be an option.

the simplest way would be to use window function for the same part,supplier and time frame to look if the max(order_type) = 'E' so in such case the order of order_type E has no corresponding E order.

for example

  • part1 with supplierAAA has just one order of order_type E so add Flag=0.

  • part2 with supplierBBB has E order and G order but the time frames doesn't match, thus Flag=0 both of them.

  • part3 with supplierCCC has 2 E orders , one in the first half of 2022 and one in 2023 full year. the E order is covered by G order in 2022 so Flag=1 both of them.

  • the E order in 2023 is covered by 2 G orders but it is covered also completely so Flag=1 all 3 orders as well.

the last case is the issue why window function will not work in partition by part,supplier,start_date,end_date , cause the dates are not aligned so it will fail to flag the orders with 1.

what option do I have? If using self join then in the ON clause I need to do something like E order start_date between G order start_date and G order end_date with OR condition to find out if the time frames fully overlap. this is not suitable to be fast enough.

here is the date as an example

 select  'part1' as part , 'supplierAAA' as supplier , 'order1' as order_name, 'E' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('31-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part2' as part , 'supplierBBB' as supplier , 'order2' as order_name, 'E' as order_type , to_date('01-01-2022','dd-mm-yyyy') as start_date , to_date('31-12-2022','dd-mm-yyyy') as end_date from dual
union all 
 select  'part2' as part , 'supplierBBB' as supplier , 'order4' as order_name, 'G' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('31-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order5' as order_name, 'E' as order_type , to_date('01-01-2022','dd-mm-yyyy') as start_date , to_date('30-06-2022','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order6' as order_name, 'E' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('31-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order7' as order_name, 'G' as order_type , to_date('01-01-2022','dd-mm-yyyy') as start_date , to_date('30-06-2022','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order8' as order_name, 'G' as order_type , to_date('01-01-2023','dd-mm-yyyy') as start_date , to_date('15-12-2023','dd-mm-yyyy') as end_date from dual
union all 
 select  'part3' as part , 'supplierCCC' as supplier , 'order9' as order_name, 'G' as order_type , to_date('16-12-2023','dd-mm-yyyy') as start_date , to_date('31-12-2024','dd-mm-yyyy') as end_date from dual

there are 2 types of order, E and G. I need to flag all orders which have for the same part and supplier and time frame a G order(s) covering an E order completely.

Because there are Millions of records I don't want to use self join and join the same data set which could be an option.

the simplest way would be to use window function for the same part,supplier and time frame to look if the max(order_type) = 'E' so in such case the order of order_type E has no corresponding E order.

for example

  • part1 with supplierAAA has just one order of order_type E so add Flag=0.

  • part2 with supplierBBB has E order and G order but the time frames doesn't match, thus Flag=0 both of them.

  • part3 with supplierCCC has 2 E orders , one in the first half of 2022 and one in 2023 full year. the E order is covered by G order in 2022 so Flag=1 both of them.

  • the E order in 2023 is covered by 2 G orders but it is covered also completely so Flag=1 all 3 orders as well.

the last case is the issue why window function will not work in partition by part,supplier,start_date,end_date , cause the dates are not aligned so it will fail to flag the orders with 1.

what option do I have? If using self join then in the ON clause I need to do something like E order start_date between G order start_date and G order end_date with OR condition to find out if the time frames fully overlap. this is not suitable to be fast enough.

Share Improve this question edited Feb 5 at 18:43 Dale K 27.3k15 gold badges56 silver badges83 bronze badges asked Feb 5 at 18:06 PatoPato 3111 silver badge10 bronze badges 2
  • Do you need to find overlapping time frames or G time frame completely encloses E time frame? The latter is easier: e.start_time BETWEEN g.start_time AND g.end_time AND e.end_time BETWEEN g.start_time AND g.end_time – Barmar Commented Feb 5 at 18:29
  • perfect answer, I fon't want a selfjoin cause too many data etc and I erote it in the requirements and you give me this.
发布评论

评论列表(0)

  1. 暂无评论