I’m new to learning oracle sql, so I’m sure this has a simple solution that I’m missing, but I’m trying to return the first sequence row from each order, not just limit the whole query results to 1 returned row. The query I’m working with is this:
select som.bi_wo_workord
, som.BI_SO_NBR
, wft.bi_wrkflw_task_seq_nbr
, wf.bi_dist_ofc_cd
, wft.bi_work_event_cd
from bi_so_master som
join bi_so_det sod on sod.bi_so_nbr = som.bi_so_nbr
join bi_wrkflw_tasks wft on wft.bi_wrkflw_key = sod.bi_wrkflw_key
join bi_wrkflw wf on wf.bi_wrkflw_key = sod.bi_wrkflw_key
where som.bi_open_dt > '01-JAN-24'
and wft.bi_work_event_cd in ('QUEUE','START')
order by som.bi_so_nbr, wft.bi_wrkflw_task_seq_nbr
which returns data like this:
"BI_WO_WORKORD","BI_SO_NBR","BI_WRKFLW_TASK_SEQ_NBR","BI_DIST_OFC_CD","BI_WORK_EVENT_CD"
"21003821","21003821",39,"HDH","QUEUE"
"21003821","21003821",39.5,"HDH","QUEUE"
"21003821","21003821",40,"HDH","QUEUE"
"21003821","21003821",41,"HDH","QUEUE"
"21003821","21003821",42,"HDH","QUEUE"
"22007385","22007385",7,"LMC","QUEUE"
"22007385","22007385",10,"LMC","QUEUE"
"22007385","22007385",11,"LMC","QUEUE"
"23002645","23002645",14,"KEP","QUEUE"
"23002645","23002645",15,"KEP","QUEUE"
"23002645","23002645",16,"KEP","QUEUE"
"23002645","23002645",17,"KEP","QUEUE"
What I’m trying to return is the first (lowest number) of each sequence number (wft.bi_wrkflw_task_seq_nbr) from each service order (som.BI_SO_NBR).
Like so:
"BI_WO_WORKORD","BI_SO_NBR","BI_WRKFLW_TASK_SEQ_NBR","BI_DIST_OFC_CD","BI_WORK_EVENT_CD"
"21003821","21003821",39,"HDH","QUEUE"
"22007385","22007385",7,"LMC","QUEUE"
"23002645","23002645",14,"KEP","QUEUE"
I’ve done some searching and tried:
select * from (
select som.bi_wo_workord
, som.BI_SO_NBR
, wft.bi_wrkflw_task_seq_nbr
, wf.bi_dist_ofc_cd
, wft.bi_work_event_cd
, sod.bi_map_loc_nbr
, row_number() over (partition by som.bi_so_nbr order by sft.bi_wrkflw_task_seq_nbr) as row_num
from bi_so_master som
join bi_so_det sod on sod.bi_so_nbr = som.bi_so_nbr
join bi_wrkflw_tasks wft on wft.bi_wrkflw_key = sod.bi_wrkflw_key
join bi_wrkflw wf on wf.bi_wrkflw_key = sod.bi_wrkflw_key
where som.bi_open_dt > '01-JAN-24'
and wft.bi_work_event_cd in ('QUEUE','START')
) as grouped_task_seq
where row_num = 1;
but this, of course, throws an error (“SQL command not properly ended”) and it feels like maybe I’m going down the wrong path and making it overly complicated.