Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MS SQL SERVER -STORED PROCEDURE


Need to group and sum on a criteria as illustrated below..

SELECT IDNO ,
LOANUMBER ,
SUM(AMOUNT) AS SUM_AMT,
TX_TYPE ,
GROUP BY IDNO, LOANUMBER


The issue is that the field TX_TYPE is (DEDUCTION,INTEREST,PRINCIPAL).


How should I organise the Select statement such the ouput will be as follows

1 IDNO
2 LOANUMBER
3 SUM_AMT_DEDUCTION
4 SUM_AMT_INTEREST
4 SUM_AMT_PRINCIPAL





How do you order on 2 fields in a stored procedure ie order on ltrim(rtrim(idno))+ltrim(rtrim(loanumber))








Thanks
Posted
Updated 17-Apr-15 8:44am
v2

1 solution

You might want to use a subquery or a Common Table Expression for each type, then do a FULL OUTER JOIN.

Or use CASE clauses.

SQL
SELECT IDNO
, LOANUMBER
, SUM(CASE TX_TYPE WHEN 'DEDUCTION' THEN AMOUNT ELSE 0 END) AS SUM_AMT_DEDUCTION
... etc
GROUP BY IDNO, LOANUMBER
 
Share this answer
 
v3
Comments
Suvendu Shekhar Giri 17-Apr-15 18:04pm    
Yes,using 'case when', it can be resolved. +5 :)

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