- 在子查询“t”中,我使用 lag 分析函数来获取上一个店员,并使用 listagg 分析函数通过订单列值获取店员列表
- 然后在最外面的查询“tt”中,我按 dep 进行分组,然后根据所需的输出创建许多计数列。
在此处使用decode(status, 'approved', 'zzzzzzzz', status) 使您在分析函数中的 order by 子句具有确定性很重要。可能存在按订单绑定的基准列值(例如,orders = 'order5')。
select tt.DEP
, count(case when clerk = 'Rick' and (prev_clerk != clerk or NB_STEPS = 1) and status = 'approved' then orders end ) "orders in single step by Rick"
, count(case when clerk = 'Rick' and prev_clerk = clerk and status = 'approved' then orders end ) "orders processed >1 by Rick"
, count(case when clerk = 'Simon' and (prev_clerk != clerk or NB_STEPS = 1) and status = 'approved' then orders end ) "orders in single step by Simon"
, count(case when clerk = 'Simon' and prev_clerk = clerk and status = 'approved' then orders end ) "orders processed >1 by Simon"
, count(case when clerk = 'Tom' and (prev_clerk != clerk or NB_STEPS = 1) and status = 'approved' then orders end ) "orders in single step by Tom"
, count(case when clerk = 'Tom' and prev_clerk = clerk and status = 'approved' then orders end ) "orders processed >1 by Tom"
, count(case when list_clerk_by_orders not like '%Rick%' then orders end) "orders without tier 1"
, count(case when clerk = 'Tom' and status = 'approved' and list_clerk_by_orders not like '%Simon%' then orders end) "withouttier2_approved_in_tier3"
from (
select t.*
, count(*)over(partition by orders)nb_steps
, lag(clerk, 1)over(partition by orders
order by datum, decode(status, 'approved', 'zzzzzzzz', status))prev_clerk
, listagg(clerk, ', ')within group(order by datum, decode(status, 'approved', 'zzzzzzzz', status))over(partition by orders)list_clerk_by_orders
from test_table t
)tt
group by tt.DEP
;
样本
create table test_table (dep, orders, datum, clerk, status) as
select 'ABC', 'order1', to_date('27.8.2020', 'dd.mm.yyyy'), 'Rick', 'checked 1' from dual union all
select 'ABC', 'order1', to_date('4.9.2020', 'dd.mm.yyyy'), 'Simon', 'checked 2' from dual union all
select 'ABC', 'order1', to_date('11.9.2020', 'dd.mm.yyyy'), 'Tom', 'approved' from dual union all
select 'ABC', 'order2', to_date('27.8.2020', 'dd.mm.yyyy'), 'Rick', 'checked 1' from dual union all
select 'ABC', 'order2', to_date('4.9.2020', 'dd.mm.yyyy'), 'Tom', 'approved' from dual union all
select 'ABC', 'order3', to_date('11.9.2020', 'dd.mm.yyyy'), 'Simon', 'approved' from dual union all
select 'ABC', 'order4', to_date('17.9.2020', 'dd.mm.yyyy'), 'Rick', 'checked 1' from dual union all
select 'ABC', 'order4', to_date('25.9.2020', 'dd.mm.yyyy'), 'Rick', 'approved' from dual union all
select 'ABC', 'order5', to_date('17.9.2020', 'dd.mm.yyyy'), 'Rick', 'checked 1' from dual union all
select 'ABC', 'order5', to_date('25.9.2020', 'dd.mm.yyyy'), 'Rick', 'checked 2' from dual union all
select 'ABC', 'order5', to_date('29.9.2020', 'dd.mm.yyyy'), 'Simon', 'approved' from dual union all
select 'ABC', 'order6', to_date('25.9.2020', 'dd.mm.yyyy'), 'Rick', 'checked 1' from dual union all
select 'ABC', 'order6', to_date('30.10.2020', 'dd.mm.yyyy'), 'Tom', 'checked 2' from dual union all
select 'ABC', 'order6', to_date('31.10.2020', 'dd.mm.yyyy'), 'Tom', 'approved' from dual
;