Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Fetch records based on status priority

$
0
0
I have a table with below syntax and insert statements.

Quote:

-- Table creation
-----------------------
create table order_test (oid number,
orderno number,
ordername varchar2(100),
orderstatus varchar2(50),
orderseq number);
----------------------------------
-- Insert statements
begin
insert into order_test values (1,100,'ORD1','INPROGRESS',1);
insert into order_test values (2,101,'ORD2','INPROGRESS',2);
insert into order_test values (3,100,'ORD1','START',2);
insert into order_test values (4,100,'ORD1','INPROGRESS',3);
insert into order_test values (5,101,'ORD2','COMPLETE',1);
insert into order_test values (6,101,'ORD2','CANCELLED',3);
insert into order_test values (7,100,'ORD1','START',4);
insert into order_test values (8,102,'ORD3','COMPLETE',1);
insert into order_test values (9,103,'ORD4','CANCELLED',1);
insert into order_test values (10,103,'ORD4','CANCELLED',2);
commit;
end;
The Output looks like below
OID|ORDERNO|ORDERNAME|ORDERSTATUS|ORDERSEQ
1 |100 |ORD1 |INPROGRESS |1
7 |100 |ORD1 |START |4
3 |100 |ORD1 |START |2
4 |100 |ORD1 |INPROGRESS |3
6 |101 |ORD2 |CANCELLED |3
2 |101 |ORD2 |INPROGRESS |2
5 |101 |ORD2 |COMPLETE |1
8 |102 |ORD3 |COMPLETE |1
9 |103 |ORD4 |CANCELLED |1
10 |103 |ORD4 |CANCELLED |2
Now, the below rules should be applied to get the output..
1) If status is same, then fetch max(seq) record
2) If status is different, then apply below priority to fetch the records
Start - Priority 1 (High)
In Progress - Priority 2
Cancelled - Priority 3
Complete - Priority 4 (Lowest)

Means, the output for each order should look like below.
Quote:

OID|ORDERNO|ORDERNAME|ORDERSTATUS|ORDERSEQ
3 |100 |ORD1 |START |2
2 |101 |ORD2 |INPROGRESS |2
8 |102 |ORD3 |COMPLETE |1
10 |103 |ORD4 |CANCELLED |2
Can somebody please suggest the ways how to do it.

Thanks.

Viewing all articles
Browse latest Browse all 13329

Trending Articles