【问题标题】:How to determine if my batches are all consecutive?如何确定我的批次是否都是连续的?
【发布时间】:2015-09-29 20:59:20
【问题描述】:

公司每年生产三批,每批使用以下命名约定:YYYY11,YYYY22,YYYY33

在这种情况下batch_id =1 所有批次都是连续的。然而,batches 200933201022 的缺失使 batch_id=2 不连续。

with batch_sequences as(
  select  1 as batch_sequence, '200911' as batch_date  from dual union all
  select  2 as batch_sequence, '200922' as batch_date from dual  union all
  select  3 as batch_sequence, '200933' as batch_date  from dual  union all
  select  4 as batch_sequence, '201011' as batch_date  from dual union all
  select  5 as batch_sequence, '201022' as batch_date from dual union all
  select  6 as batch_sequence, '201033' as batch_date  from dual),
batch_entries as
 (
   select   1 as batch_id, '200911' as  batch_date from dual union all
   select   1 as batch_id, '200922' as  batch_date from dual union all
   select   1 as batch_id, '200933' as  batch_date from dual union all
   select   1 as batch_id, '201011' as  batch_date from dual union all
   select   1 as batch_id, '201022' as  batch_date from dual union all
   select   1 as batch_id, '201033' as  batch_date from dual union all
   select   2 as batch_id, '200911' as  batch_date from dual union all
   select   2 as batch_id, '200922' as  batch_date from dual union all
   select   2 as batch_id, '201011' as  batch_date from dual union all
   select   2 as batch_id, '201033' as  batch_date from dual
 )
select batch_sequence,
       e.batch_id,
       s.batch_date,
       lead(batch_sequence,1) over (order by batch_sequence) as next_batch
  from batch_entries e
 inner join batch_sequences s on e.batch_date=s.batch_date
 order by e.batch_id,
          e.batch_date;

我想我可以对潜在客户值进行数学运算,但我没有得到所有 batch_sequence 值来正确计算。

问题

如何编写查询以显示 batch_id=1 有一个“完美运行”并且 batch_id=2 错过了一些批处理日期?

我会满足于任何可以突出显示这一点的结果集。

【问题讨论】:

  • batch_date 究竟是字符串还是数字?
  • batch_date 是一个字符串 (varchar2(6))。
  • 如果它应该是完全相同的批次数量,那就是简单的COUNT?
  • 那行不通,因为我不一定知道 batch_id 何时开始或结束生产。它可能会错过一个 batch_date,然后再次恢复。 batch_id=1,201033 的缺失仍然会使 batch_id=1 运行完美。
  • 我需要将每个batch_id 与batch_sequence 数字进行比较,而不是将一个batch_id 的count() 与另一个batch_id 的count() 进行比较。

标签: sql oracle window-functions


【解决方案1】:

根据batch_date为每个batch_id分配一个序号,并与batch_sequence进行比较:

with cte as
 (
   select batch_id, batch_date,
      row_number() -- sequential number
      over (partition by batch_id
            order by batch_date) as rn
   from batch_entries
 )
 select e.batch_id
 from cte e join batch_sequences s 
   on e.batch_date=s.batch_date 
 group by e.batch_id
 -- if there's no missing batch the difference will always be the same  
 having min(s.batch_sequence - e.rn) <> max(s.batch_sequence - e.rn)

fiddle

第二批数据:

batch_date  rn    batch_sequence  batch_date
'200911'  -> 1           1         '200911'
'200922'  -> 2           2         '200922'
                         3         '200922'
'201011'  -> 3           4         '201011' 
                         5         '201022'
'201033'  -> 4           6         '201033'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多