Question: How can I assign a code such as CC=1 and WP=2 and make that part of the the query to get desired results? If there are multiple transactions on the same transaction date then apply the business rule.
Here’s my query on Oracle 11g
select tid,
cycle,
apply_no,
seq_no,
trans_date,
trans_type,
priority
from ( select tid,
cycle,
apply_no,
seq_no,
trans_date,
trans_type,
rank() over (partition by tid,
order by trans_date desc,seq_no desc) priority
from transactions where tid=1
Current Results
TID cycle apply_no seq_no trans_date trans_type priority
----------------------------------------------------------------
1 201420 2 2 27-NOV-12 WP 1
1 201320 1 1 27-NOV-12 CC 2
1 201420 2 1 16-OCT-12 CC 3
Desired Results
TID cycle apply_no seq_no trans_date trans_type priority
---------------------------------------------------------------
1 201420 2 2 27-NOV-12 CC 1
1 201320 1 1 27-NOV-12 WP 2
1 201420 2 1 16-OCT-12 CC 3
Reason: Business rules state CC takes priority over WP (not because CC sorts before WP)
if trans_date is same.
Since your trans_type order is actually alphabetical, you could just add that to your order by in the rank function. However, to be more clear I would use a decode.. something like:
Which produces: