Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the result set you can see we have 2 claimkeys , i want to replace 2nd row primarypayment column value (ie 116 ) with 1st row primarypayment column so i need result in one row.


SQL
SELECT    distinct ecpc.ecpclaim_key AS claim_key
    ,ecpc.ecpclaim_patient_last_name AS patient_last_name
    ,ecpc.ecpclaim_patient_first_name AS patient_first_name
    ,(SELECT MIN(ecpservice_date_from) FROM tbl_ecpserviceline WHERE ecpclaim_key = ecpc.ecpclaim_key) AS DOS
    ,ecpc.ecpclaim_carrier_id AS payer_id
    ,ecpc.eCPClaim_carrier_name AS payer_name
    ,(CASE WHEN eCPLedger_Master_Transaction_Type IN (1001) THEN ecpledger_master_transaction_amount END )  AS Charges
    ,(CASE WHEN eCPLedger_Master_Transaction_Type IN (1004,1007) THEN (SELECT DISTINCT ARRAY_TO_STRING(ARRAY_AGG(ecpledger_master_transaction_payment ), '') FROM  tbl_ecpledger_master WHERE eCPLedger_Master_Transaction_Type IN (1004,1007) and ecpc.ecpclaim_key=ecpclaim_key )
     END ) AS PrimayPayment
    ,LM.ecpledger_master_claim_balance

    FROM
        tbl_ecpclaim ecpc
        inner JOIN tbl_ecpledger_master LM  ON ecpc.ecpclaim_key=LM.ecpclaim_key


    WHERE ecpc.ecpclaim_insurancetype='P'

    GROUP BY ecpc.ecpclaim_key,lm.ecpledger_master_claim_balance,lm.ecpledger_master_transaction_type,lm.ecpledger_master_transaction_amount limit 10


Result for above query
Claim P_l P_F DOS Payer_i Payer_Name Charges PrimaryPayment Balance
18801 Lyons Wendy 2014 23071 Alliance 116 NULL 0
18801 Lyons Wendy 2014 23071 Alliance 116 NULL

Excepted out put

Claim P_l P_F DOS Payer_id Payer_Name Charges PrimaryPayment Balance
18801 Lyons Wendy 2014 23071 Alliance 116 116 0
Posted
Updated 10-Dec-14 20:01pm

1 solution

Just add ISNULL to your balance and it will create two zeros which will be eliminated by DISTINCT. I hope this helps (change in bold)

SQL
SELECT    distinct ecpc.ecpclaim_key AS claim_key
    ,ecpc.ecpclaim_patient_last_name AS patient_last_name
    ,ecpc.ecpclaim_patient_first_name AS patient_first_name
    ,(SELECT MIN(ecpservice_date_from) FROM tbl_ecpserviceline WHERE ecpclaim_key = ecpc.ecpclaim_key) AS DOS
    ,ecpc.ecpclaim_carrier_id AS payer_id
    ,ecpc.eCPClaim_carrier_name AS payer_name
    ,(CASE WHEN eCPLedger_Master_Transaction_Type IN (1001) THEN ecpledger_master_transaction_amount END )  AS Charges
    ,(CASE WHEN eCPLedger_Master_Transaction_Type IN (1004,1007) THEN (SELECT DISTINCT ARRAY_TO_STRING(ARRAY_AGG(ecpledger_master_transaction_payment ), '') FROM  tbl_ecpledger_master WHERE eCPLedger_Master_Transaction_Type IN (1004,1007) and ecpc.ecpclaim_key=ecpclaim_key )
     END ) AS PrimayPayment
    ,COALESCE(LM.ecpledger_master_claim_balance, 0.00) as balance

    FROM
        tbl_ecpclaim ecpc
        inner JOIN tbl_ecpledger_master LM  ON ecpc.ecpclaim_key=LM.ecpclaim_key


    WHERE ecpc.ecpclaim_insurancetype='P'

    GROUP BY ecpc.ecpclaim_key,lm.ecpledger_master_claim_balance,lm.ecpledger_master_transaction_type,lm.ecpledger_master_transaction_amount limit 10
 
Share this answer
 
v3
Comments
Member 10256268 10-Dec-14 3:17am    
if i execute your query i am getting this erro function isnull(double precision, integer) does not exist
Sinisa Hajnal 10-Dec-14 3:49am    
I used the function from SQL Server (tag was originally SQL, wasn't it!?) - simply find the equivalent function (maybe NVL like in Oracle or COALESCE?)
Edited.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900