tags:

views:

28

answers:

1

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 ')

A: 

...ok, assuming that some fields are grouped into the same table (if not you'll just have to write the joins), you just need 1 query. (I wish all languages were as concise...) @AccountId is your desired account id.

SELECT l.LOAN_NUMBER, l.AccountId,
    (SELECT 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) 
      as ExpenseTotal FROM Expenses_Guessing_The_Table_Name
      WHERE AccountId = @AccountId) as ExpenseTotal,
    (SELECT sum(NET_PAY_AMOUNT) + sum(OTHER_INCOME_AMOUNT) as IncomeTotal
      FROM Income_Guessing_The_Table_Name
      WHERE AccountId = @AccountId) as IncomeTotal
    FROM Loans l 
    WHERE  l.AccountId = @AccountId AND l.Balance > 0
Tahbaza
Yes all the columns live in the same table. The second table is only to validate the account still has a balance.I will give this a whirl, So appreciate the help..
JMS49
received this error:SQL0104N An unexpected token "as" was found following "WHERE F.Loan_Number". Expected tokens may include: "<interval_qualifier>". SQLSTATE=42601
JMS49
here is code: SELECT LOAN_NUMBER, (SELECT 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) as ExpenseTotal FROM Financials F WHERE F.Loan_Number as ExpenseTotal, (SELECT sum(NET_PAY_AMOUNT) + sum(OTHER_INCOME_AMOUNT) as IncomeTotal FROM Financials WHERE L.Loan_Number as IncomeTotal FROM Loan L WHERE L.First_Principal_Balance > 0
JMS49
You're missing a closing parenthesis before the "AS ExpenseTotal"
Ken Keenan
Tried again SELECT LOAN_NUMBER, (SELECT 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) as ExpenseTotal) FROM FINANCIALS (SELECT sum(NET_PAY_AMOUNT) + sum(OTHER_INCOME_AMOUNT) as IncomeTotal) FROM FINANCIALS FROM LOAN L WHERE L.First_Principal_Balance > 0
JMS49
New Error: SQL0104N An unexpected token ")" was found following "as ExpenseTotal". Expected tokens may include: "<table_expr>". SQLSTATE=42601".
JMS49
The code I pasted above is not what you're trying. Looking at what you're pasting now, try this: SELECT LOAN_NUMBER, (SELECT 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) FROM FINANCIALS) as ExpenseTotal,(SELECT sum(NET_PAY_AMOUNT) + sum(OTHER_INCOME_AMOUNT) FROM FINANCIALS) as IncomeTotalFROM LOAN LWHERE L.First_Principal_Balance > 0
Tahbaza
One more crucial thing that you dropped in my initial post and I missed in my latest response... You need to add a where clause entry to each subquery to filter by AccountID (or LoanID?) to force the filter by just that loan number. Otherwise you'll get the same 2 numbers reported for each row. [WHERE AccountId = @AccountId]
Tahbaza
I think matched yours,expense SELECT F.LOAN_NUMBER, (SELECT(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_1_AMOUNT) + SUM(EXP_USER_2_AMOUNT) + SUM(EXP_USER_3_AMOUNT) + SUM(EXP_UTILITIES_AMOUNT)FROM FINANCIAL F) AS ExpenseTotal, (SELECT(SUM(MORTGAGOR_NET_PAY_AMOUNT) + SUM(MORTGAGOR_OTHER_INCOME_AMOUNT) AS IncomeTotal FROM FINANCIAL F INNER JOIN BDE.LOAN_V a ON F.LOAN_NUMBER = A.LOAN_NUMBERWHERE A.FIRST_PRINCIPAL_BALANCE> 0 GROUP BY F.LOAN_NUMBER, ORDER BY F.LOAN_NUMBER
JMS49
new error OLE DB provider "MSDASQL" for linked server "LINKBDE" returned message "[IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "FROM" was found following "XP_UTILITIES_AMOUNT)". Expected tokens may include: ")". SQLSTATE=42601".Msg 7350, Level 16, State 2, Line 4Cannot get the column information from OLE DB provider "MSDASQL" for linked server "LINKBDE".
JMS49
Hey JMS49, it looks like you're using a linked server to DB2 to pass SQL through AND you're possibly using a custom extended procedure XP_UTILITIES_AMOUNT. I should have seen the "FETCH ONLY WITH UR" bit and understood the DB2 slant, my bad. The next step for you is to figure out where XP_UTILITIES_AMOUNT is getting invoked (not in any code you've posted) and tracing that error down. Good luck.
Tahbaza