Search code examples
sqlsql-servercursorprocedure

How to sum up the cursor value and store the value in another select statement in the same procedure?


I have two tables, one is general table and another is loan table.

General table includes id and name and id is not repeating.

Loan table includes id and loan amount. In loan table, the id may be repeating as one customer can have multiple loans.

Idea is to create a procedure which will join this both the table on the basis of id and display the final table with id, name and sum of all loans for that particular id (must sum up the value of common id's and store in single column) with the help of cursor.

Can only use procedure and cursor.

If it can be solved without cursor, then please provide the alternate solution.

I am using below query for the same, but unfortunately the end result displays the id multiple times:

SELECT
A.bpm_referenceno,
CASE WHEN B.loanbookingbranch='--Select--' or B.loanbookingbranch='null' THEN '' ELSE B.loanbookingbranch END,
CASE WHEN A.branch='' OR A.branch IS NULL OR A.branch='null' THEN '' ELSE A.branch END,
'',
CASE WHEN A.originator='' OR A.originator IS NULL OR A.originator='null' THEN '' ELSE A.originator END,
A.cif_id,
CASE WHEN D.callexecutiondate='' OR D.callexecutiondate IS NULL OR D.callexecutiondate ='null' THEN '' ELSE CASE WHEN D.calldescription='MG Contract Creation' AND D.callstatus='SUCCESS' THEN D.callexecutiondate END END,
CASE WHEN A.originationdate IS NULL THEN '' ELSE A.originationdate END,
CASE WHEN A.request_type='' OR A.request_type IS NULL OR A.request_type='null' THEN '' ELSE A.request_type END,
CASE WHEN a.loan_subtype='' OR a.loan_subtype IS NULL OR a.loan_subtype='null' THEN '' ELSE a.loan_subtype END,
CASE WHEN E.loanamounttxndetails IS NULL OR E.loanamounttxndetails ='null' OR E.loanamounttxndetails='' THEN '0' ELSE e.loanamounttxndetails END,
CASE WHEN E.customerdbrtxndetails IS NULL OR E.customerdbrtxndetails ='null' OR E.customerdbrtxndetails='' THEN '0.00' ELSE E.customerdbrtxndetails END,
'stlment count',
'loan os',
--Here I am inserting the query--
(SELECT
isnull(ab.mycount,0)
FROM
bm_rlos_exttable aa
LEFT OUTER JOIN
(SELECT bpm_referenceno,count(bpm_referenceno) mycount FROM
BM_RLOS_ExistingBMLiabilitiesGrid GROUP BY bpm_referenceno) ab ON aa.bpm_referenceno = ab.bpm_referenceno),
CASE WHEN G.isselected='true' THEN G.insuranceType ELSE '' END,
CASE WHEN E.loantenortxndetails IS NULL OR E.loantenortxndetails='' OR E.loantenortxndetails='null' THEN '' ELSE E.loantenortxndetails END,
CASE WHEN E.interestratetxndetails IS NULL OR E.interestratetxndetails='' OR E.interestratetxndetails='null' THEN '' ELSE E.interestratetxndetails END,
'CHARGE1',
'CHARGE2',
CASE WHEN D.calldescription='MG Contract Creation' AND D.callstatus='SUCCESS' THEN D.callreferenceid ELSE '' END,
'rco',
'tat'

FROM
BM_RLOS_EXTTABLE A WITH (NOLOCK)
INNER JOIN BM_RLOS_BasicLoanDetailsForm B WITH (NOLOCK)
ON A.bpm_referenceno = B.bpm_referenceno
INNER JOIN BM_RLOS_DisbursementCallsGrid D WITH (NOLOCK)
ON A.bpm_referenceno = D.bpm_referenceno
INNER JOIN BM_RLOS_CheckFinalEligibilityForm E WITH (NOLOCK)
ON A.bpm_referenceno = E.bpm_referenceno
INNER JOIN BM_RLOS_ExistingBMLiabilitiesGrid F WITH (NOLOCK)
ON A.bpm_referenceno = F.bpm_referenceno 
INNER JOIN BM_RLOS_InsuranceProductSelectionGrid G WITH (NOLOCK)
ON A.bpm_referenceno = G.bpm_referenceno 
INNER JOIN BM_RLOS_ChargeAndFeeDetailsForm H WITH (NOLOCK)
ON A.bpm_referenceno = H.bpm_referenceno
INNER JOIN BM_RLOS_DecisionHistoryForm I WITH (NOLOCK)
ON A.bpm_referenceno = I.bpm_referenceno 
INNER JOIN wfcurrentroutelogtable J WITH (NOLOCK)
ON A.bpm_referenceno = J.ProcessInstanceId

Solution

  • This should do the trick

    SELECT
    a.id,
    a.name,
    b.mysum
    FROM
    General_Table a 
    LEFT OUTER JOIN (select bpm_id, SUM(outstandingamount) mysum 
                     from loan_table group by bpm_id) b
    ON a.id = b.bpm_id
    

    Edit: subquery changed ( based on your join clause i assumed your id field in the loan_table was bpm_id)

    Edit2:
    Note i moved your nested query into a new LEFT JOIN in order to make this work. You cannot use nested query in a select statement if it returns more then 1 record

    SELECT
    A.bpm_referenceno,
    CASE WHEN B.loanbookingbranch='--Select--' or B.loanbookingbranch='null' THEN '' ELSE B.loanbookingbranch END,
    CASE WHEN A.branch='' OR A.branch IS NULL OR A.branch='null' THEN '' ELSE A.branch END,
    '',
    CASE WHEN A.originator='' OR A.originator IS NULL OR A.originator='null' THEN '' ELSE A.originator END,
    A.cif_id,
    CASE WHEN D.callexecutiondate='' OR D.callexecutiondate IS NULL OR D.callexecutiondate ='null' THEN '' ELSE CASE WHEN D.calldescription='MG Contract Creation' AND D.callstatus='SUCCESS' THEN D.callexecutiondate END END,
    CASE WHEN A.originationdate IS NULL THEN '' ELSE A.originationdate END,
    CASE WHEN A.request_type='' OR A.request_type IS NULL OR A.request_type='null' THEN '' ELSE A.request_type END,
    CASE WHEN a.loan_subtype='' OR a.loan_subtype IS NULL OR a.loan_subtype='null' THEN '' ELSE a.loan_subtype END,
    CASE WHEN E.loanamounttxndetails IS NULL OR E.loanamounttxndetails ='null' OR E.loanamounttxndetails='' THEN '0' ELSE e.loanamounttxndetails END,
    CASE WHEN E.customerdbrtxndetails IS NULL OR E.customerdbrtxndetails ='null' OR E.customerdbrtxndetails='' THEN '0.00' ELSE E.customerdbrtxndetails END,
    'stlment count',
    'loan os',
    --Here I am inserting the query--
    mycount,
    CASE WHEN G.isselected='true' THEN G.insuranceType ELSE '' END,
    CASE WHEN E.loantenortxndetails IS NULL OR E.loantenortxndetails='' OR E.loantenortxndetails='null' THEN '' ELSE E.loantenortxndetails END,
    CASE WHEN E.interestratetxndetails IS NULL OR E.interestratetxndetails='' OR E.interestratetxndetails='null' THEN '' ELSE E.interestratetxndetails END,
    'CHARGE1',
    'CHARGE2',
    CASE WHEN D.calldescription='MG Contract Creation' AND D.callstatus='SUCCESS' THEN D.callreferenceid ELSE '' END,
    'rco',
    'tat'
    FROM
    BM_RLOS_EXTTABLE A WITH (NOLOCK)
    INNER JOIN BM_RLOS_BasicLoanDetailsForm B WITH (NOLOCK)
    ON A.bpm_referenceno = B.bpm_referenceno
    INNER JOIN BM_RLOS_DisbursementCallsGrid D WITH (NOLOCK)
    ON A.bpm_referenceno = D.bpm_referenceno
    INNER JOIN BM_RLOS_CheckFinalEligibilityForm E WITH (NOLOCK)
    ON A.bpm_referenceno = E.bpm_referenceno
    INNER JOIN BM_RLOS_ExistingBMLiabilitiesGrid F WITH (NOLOCK)
    ON A.bpm_referenceno = F.bpm_referenceno 
    INNER JOIN BM_RLOS_InsuranceProductSelectionGrid G WITH (NOLOCK)
    ON A.bpm_referenceno = G.bpm_referenceno 
    INNER JOIN BM_RLOS_ChargeAndFeeDetailsForm H WITH (NOLOCK)
    ON A.bpm_referenceno = H.bpm_referenceno
    INNER JOIN BM_RLOS_DecisionHistoryForm I WITH (NOLOCK)
    ON A.bpm_referenceno = I.bpm_referenceno 
    INNER JOIN wfcurrentroutelogtable J WITH (NOLOCK)
    ON A.bpm_referenceno = J.ProcessInstanceId
    left join
    (SELECT bpm_referenceno,count(bpm_referenceno) mycount FROM
    BM_RLOS_ExistingBMLiabilitiesGrid GROUP BY bpm_referenceno) ab ON ab.bpm_referenceno = a.bpm_referenceno)