DENSE_RANK() returns multiple records instead of one record.
773020Jul 2 2010 — edited Jul 4 2010select *
from(
select carordercontextdetail.OFFER_OC_SUBTOTAL_AMT as OFFER_OC_SUBTOTAL_AMT,
carordercontextdetail.OFFER_RC_SUBTOTAL_AMT as OFFER_RC_SUBTOTAL_AMT,
carordercontextdetail.CONTRACT_TERM_DURATION as CONTRACT_TERM_DURATION,
carordercontextdetail.CNTRCT_TERM_REMAIN_IN_MTH as CNTRCT_TERM_REMAIN_IN_MTH,
car.CREDIT_ASSESSMENT_REQUEST_ID,
car.CREATE_TS,DENSE_RANK() OVER
(ORDER BY car.CREATE_TS DESC NULLS LAST) AS OFFER
from CREDIT_ASSESSMENT_REQUEST car
inner join CAR_ORDER_CONTEXT carordercontext on
carordercontext.CREDIT_ASSESSMENT_REQUEST_ID = car.CREDIT_ASSESSMENT_REQUEST_ID
inner join CAR_ORD_CNTXT_DTL carordercontextdetail on
carordercontextdetail.CAR_ORDER_CONTEXT_ID = carordercontext.CAR_ORDER_CONTEXT_ID
inner join CAR_ATTR_VALUE carattrvalue on
carattrvalue.CREDIT_ASSESSMENT_REQUEST_ID = carordercontext.CREDIT_ASSESSMENT_REQUEST_ID
where
carattrvalue.CAR_ATTR_TYP_CD = 'MASTER_CUST_ID'
and carattrvalue.CAR_ATTR_VALUE_TXT =112431188
and carattrvalue.EFF_STOP_TS = to_date('99991231','YYYYMMDD')
and carordercontextdetail.OFFER_ID ='offer#01'
and carordercontextdetail.WIRELSS_PRODUCT_IND ='N'
and car.CREATE_TS < (select CREATE_TS from CREDIT_ASSESSMENT_REQUEST
where CREDIT_ASSESSMENT_REQUEST_ID= 1115)
order by car.CREATE_TS DESC nulls last)
WHERE OFFER = 1
This query returns multiple records.How do pick the first row of record?
The reason is ,it ranks for both rows are same.For both rows,rank is 1.That's the reason it gives me multiple row.
The thing is,CREDIT_ASSESSMENT_REQUEST has unique key CREDIT_ASSESSMENT_REQUEST_ID and unique CREATE_TS,But when join with CAR_ORD_CNTXT_DTL
CAR_ORD_CNTXT_DTL has multiple offer_id maps to one CREDIT_ASSESSMENT_REQUEST_ID .
If there are three offer ids for one CREDIT_ASSESSMENT_REQUEST_ID ,it returns me three records after executing this query.
Since CREDIT_ASSESSMENT_REQUEST_ID is inserted with offer ids ,but CREDIT_ASSESSMENT_REQUEST_ID 's having only one CREATE_TS ,but offer_id is in CAR_ORD_CNTXT_DTL gets inserted one by one,it has different CREATE_TS with difference of fraction of milliseconds.
How do I pick the first row from the above query?
Do I need to fix any issues with this query?