[highlight]create sequence grpid[/highlight]
select
ord
, opr
, wc
, ck
, tx
, oai
[highlight], case when oai='OUT to Vendor' then grpid.nextval else grpid.curval end grp[/highlight]
from
(
select
ord
, opr
, wc
, ck
, tx
, case when wc like '%35Y' then 'IN from Vendor'
when ck = 'ZBOP' then 'IN from Vendor'
when tx like '%INSPECT%' then 'IN from Vendor'
when wc like '%OUT' then 'OUT to Vendor'
when ck = 'ZBPR' then 'OUT to Vendor'
when tx like '%OFFLOAD%' then 'OUT to Vendor'
when next_wc like '%OUT' then 'IN from Vendor'
when next_ck = 'ZBPR' then 'IN from Vendor'
when prev_wc like '%35Y' then 'OUT to Vendor'
when prev_ck = 'ZBOP' then 'OUT to Vendor'
when prev_tx like '%INSPECT%' then 'OUT to Vendor'
else 'AT Vendor'
end OAI
from
(
SELECT
substr(oh.ORDER_NO,length(oh.ORDER_NO)-9,10)||'-'||op.SEQUENCE_APLFL ord
, op.OPERATION opr
, wc.WORK_CNTR wc
, op.CNTL_KEY ck
, op.OPR_SHORT_TEXT tx
, lag (wc.work_cntr) over(partition by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL order by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL, op.OPERATION) prev_wc
, lead (wc.work_cntr) over(partition by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL order by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL, op.OPERATION) next_wc
, lag (op.CNTL_KEY) over(partition by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL order by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL, op.OPERATION) prev_ck
, lead (op.CNTL_KEY) over(partition by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL order by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL, op.OPERATION) next_ck
, lag (op.OPR_SHORT_TEXT) over(partition by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL order by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL, op.OPERATION) prev_tx
, lead (op.OPR_SHORT_TEXT) over(partition by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL order by oh.ORDER_NO||'-'||op.SEQUENCE_APLFL, op.OPERATION) next_tx
FROM
SIX_SIGMA.VS_AFKO_ORDER_HEADER oh
, SIX_SIGMA.VS_AFVC_ORDER_OPERATIONS op
, SIX_SIGMA.VS_CRHD_WORK_CENTER wc
WHERE oh.PLAN_NO = op.PLAN_NUM
AND wc.RESOURCE_OBJ_ID = op.RESOURCE_OBJ_ID
AND (substr(wc.WORK_CNTR,1,3) In ('161','165','167') or wc.WORK_CNTR='35Y0035Y')
and oh.actual_rel_dt >'00000000'
and substr(oh.ORDER_NO,length(oh.ORDER_NO)-9,10)='1000038915-000000'
ORDER BY
substr(oh.ORDER_NO,length(oh.ORDER_NO)-9,10)||'-'||op.SEQUENCE_APLFL
, op.OPERATION
)
)