如何确定我的批次是否都是连续的?
How to determine if my batches are all consecutive?
公司每年生产三批,每批使用以下命名约定:YYYY11,YYYY22,YYYY33
在这种情况下 batch_id =1
所有批次都是连续的。然而 batches 200933
和 201022
的缺失使得 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
有一个 'perfect run' 而 batch_id=2
错过了一些 batch_dates?
我会满足于任何可以突出这一点的结果集。
根据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)
第二批数据:
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'
公司每年生产三批,每批使用以下命名约定:YYYY11,YYYY22,YYYY33
在这种情况下 batch_id =1
所有批次都是连续的。然而 batches 200933
和 201022
的缺失使得 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
有一个 'perfect run' 而 batch_id=2
错过了一些 batch_dates?
我会满足于任何可以突出这一点的结果集。
根据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)
第二批数据:
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'