Wednesday, January 18, 2017

Query to find customer name from account ID in CCB

This query help you to retrieve customer name from his account id

SELECT CPN.ENTITY_NAME
FROM CI_ACCT CA
INNER JOIN CI_ACCT_PER CAP
ON (CAP.ACCT_ID = CA.ACCT_ID
AND CA.ACCT_ID = '0002000000' -- Put your account id here
AND TRIM(CAP.ACCT_REL_TYPE_CD) = 'MAIN')-- only to retrieve main customers there may be one or many child
INNER JOIN CI_PER CP
ON (CP.PER_ID = CAP.PER_ID)
INNER JOIN CI_PER_NAME CPN
ON (CPN.PER_ID = CP.PER_ID
AND TRIM(CPN.NAME_TYPE_FLG) = 'PRIM');--only to retrieve primary name may or may not there are many

Query to retrieve account from meter id in CCB

SELECT AC.ACCT_ID
FROM CI_MTR MTR
INNER JOIN CI_MTR_CONFIG CNF
ON (MTR.MTR_ID = CNF.MTR_ID
AND MTR.MTR_ID = 0000084706)-- put your mtr id here
INNER JOIN CI_SP_MTR_HIST HIST
ON (CNF.MTR_CONFIG_ID = HIST.MTR_CONFIG_ID
AND TRIM(HIST.REMOVAL_MR_ID) IS NULL) --When you have to retrieve the meter which is installed on sp this field will be null
INNER JOIN CI_SP SP
ON(HIST.SP_ID = SP.SP_ID)
INNER JOIN CI_SA_SP SASP
ON (SP.SP_ID = SASP.SP_ID)
INNER JOIN CI_SA SA
ON(SASP.SA_ID = SA.SA_ID
AND SA.SA_STATUS_FLG = '20')--for retrieval of active SA's
INNER JOIN CI_ACCT AC
ON(SA.ACCT_ID = AC.ACCT_ID);