I am really new with MSSQL and Full Stack development I want to perform UNION ALL with 4 different tables for a loan-savings management system The following Code is a stored procedure I have for the 4 tables im getting the error
Msg 207, Level 16, State 1, Procedure USP_GetLoanSavingsDetailsByLoanTakerId, Line 13 Invalid column name 'GetSavings'.
Msg 207, Level 16, State 1, Procedure USP_GetLoanSavingsDetailsByLoanTakerId, Line 13 Invalid column name 'GiveSavings`
And this is my code I want to achieve the Loan Given to a person,Loan Received from a person,
Savings Taken from a person and Savings Given to a person maintaining the transaction on the same day
MemberId
is the person I am taking the loan and giving the savings to and UserId
is the person who wants to give the loan to the member and get/give savings to that same person
ALTER PROCEDURE [dbo].[USP_GetLoanSavingsDetailsByLoanTakerId]
(
@MemberId bigint,
@UserId bigint
)
AS
BEGIN
--EXEC USP_GetLoanSavingsDetailsByLoanTakerId 5, 26
SELECT EntryDate, SUM(LoanGiven) AS LoanGiven, SUM(LoanReceived) AS LoanReceived , SUM(GetSavings) AS GetSavings, SUM(GiveSavings) AS GiveSavings
FROM(
SELECT EntryDate, TotalAmount as LoanGiven, 0 as LoanReceived
FROM tblGiveLoan
WHERE UserId = @UserId and MemberId = @MemberId
UNION ALL
SELECT EntryDate, 0 as LoanGiven, Amount as LoanReceived
FROM tblReceiveLoan
WHERE UserId = @UserId and MemberId = @MemberId
UNION ALL
SELECT EntryDate, TotalAmount as GetSavings, 0 as GiveSavings
FROM tblGetSavings
WHERE UserId = @UserId and MemberId = @MemberId
UNION ALL
SELECT EntryDate, 0 as GetSavings , TotalAmount as GiveSavings
FROM tblGiveSavings
WHERE UserId = @UserId and MemberId = @MemberId
) t
GROUP BY EntryDate
ORDER BY EntryDate ASC
END