【发布时间】:2015-09-29 20:59:20
【问题描述】:
公司每年生产三批,每批使用以下命名约定: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 有一个“完美运行”并且 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