I am trying to total by account, the expenses & income per account. There are multiples of both the income & the expenses per account. I am struggling with this as am still learning SQL and thought that someone else likely has already addressed this? I sure would appreciate the help!
I know that this SQL server code is not correct but it at least gives a bit clearer picture of what I am attempting to do.
IF(SELECT(OBJECT_ID('TEMPDB..#Total'))) IS NOT NULL DROP TABLE #Total
declare @Expenses decimal(13,2), @income decimal(13,2)
set @expenses = sum(EXP_CHILD_CARE_AMOUNT) + sum(EXP_FOOD_AMOUNT) + sum(EXP_LIFE_INSURANCE_AMOUNT) + sum(EXP_TRANSPORTATION_AMOUNT) + sum(EXP_TUITION_AMOUNT) + sum(EXP_USER_2_AMOUNT) + sum(EXP_USER_3_AMOUNT) + sum(EXP_UTILITIES_AMOUNT)
set @income = (sum(NET_PAY_AMOUNT) + sum(OTHER_INCOME_AMOUNT)
SELECT F.LOAN_NUMBER, @Income, @Expenses
INTO #Total FROM OPENQUERY(SvrLink, '
SELECT F.Account, @Income, @Expenses
FROM finances F inner join account a on(a.Account = f.Account) where a.balance > 0
FETCH ONLY WITH UR ')