id current stage previous stages
1 06 05
1 06 03
2 04 03
2 04 02
假设有5个阶段的id(02,03,04等)。 id应该经历每个阶段。这里的示例Id = 1跳过04和02阶段,但id = 2跳过所有。所以它应该是当前阶段-1和-2等。
我必须识别跳过阶段的这类ID。我需要使用PostgreSQL查询。
答案 0 :(得分:2)
假设当前/先前每个id都是唯一的:
select id
from tab
group by id
having min(previous_stage) <> 2 -- doesn't start with 2
or max(current_stage) - 2 <> count(*) -- there's at least one missing stage
编辑:
如果distinct
previous_stage
中的id
不唯一,则应用select id
from tab
group by id
having min(previous_stage) <> 2 -- doesn't start with 2
or max(current_stage) - 2 <> count(distinct previous_stage) -- there's at least one missing stage
。
or
编辑:
我之前的查询逻辑错误,应该是and
而不是select id
from tab
group by id
having not
-- these are the correct ids
( min(previous_stage) = 2 -- start with 2
and max(current_stage) - min(previous_stage) = count(distinct previous_stage) -- no missing steps
and max(previous_stage) = max(current_stage) -1 -- no greater step
)
。
这应该涵盖您的要求:
[PROMO_CONSENT_FORM]: {
label: _t(panelMessages.promos),
data: Promo,
form: {
formId: PROMO_CONSENT_FORM,
data: {
[PROMO_CONSENT]: promo_consent
}
}
}