Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Loop through Table

$
0
0

How would I make this query output the results for each account number in the table ?

CREATE PROCEDURE [FinalReport]
@AccNum nvarchar(50)
SET @AccNum=(SELECT DISTINCT(AccountNumber) FROM [Transaction Table])
AS 
BEGIN
SELECT 
BranchCode,
AccountType,
Status,
(COUNT(BranchCode))AS 'TOTAL COUNT',
(SUM (CAST(Amount AS FLOAT))) AS 'Total Amoun'
FROM [Transaction Table]WHERE AccountType='Savings' AND Status='Successful' AND BranchCode=@AccNum
group by
Status,
AccountType,
BranchCode

UNION ALL

SELECT 
BranchCode,
AccountType,
Status,
(COUNT(BranchCode))AS 'TOTAL COUNT',
(SUM (CAST(Amount AS FLOAT))) AS 'Total Amoun'
FROM [Transaction Table]WHERE AccountType='Cheque' AND Status='Successful' AND BranchCode=@AccNum
group by
Status,
AccountType,
BranchCode

UNION ALL

SELECT 
BranchCode,
AccountType,
Status,
(COUNT(BranchCode))AS 'TOTAL COUNT',
(SUM (CAST(Amount AS FLOAT))) AS 'Total Amoun'
FROM [Transaction Table]WHERE AccountType='Savings' AND Status='Disputed' AND BranchCode=@AccNum
group by
Status,
AccountType,
BranchCode

UNION ALL

SELECT 
BranchCode,
AccountType,
Status,
(COUNT(BranchCode))AS 'TOTAL COUNT',
(SUM (CAST(Amount AS FLOAT))) AS 'Total Amoun'
FROM [Transaction Table]WHERE AccountType='Cheque' AND Status='Disputed' AND BranchCode=@AccNum
group by
Status,
AccountType,
BranchCode

UNION ALL

SELECT 
BranchCode,
AccountType,
Status,
(COUNT(BranchCode))AS 'TOTAL COUNT',
(SUM (CAST(Amount AS FLOAT))) AS 'Total Amoun'
FROM [Transaction Table]WHERE AccountType='Savings' AND Status='Failed' AND BranchCode=@AccNum
group by
Status,
AccountType,
BranchCode

UNION ALL

SELECT 
BranchCode,
AccountType,
Status,
(COUNT(BranchCode))AS 'TOTAL COUNT',
(SUM (CAST(Amount AS FLOAT))) AS 'Total Amoun'
FROM [Transaction Table]WHERE AccountType='Cheque' AND Status='Failed' AND BranchCode=@AccNum
group by
Status,
AccountType,
BranchCode
END


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>