0

tableA:

tranactionid staffid amount 
1001          19052   2000
1002          19043   3000
1004          19076   4000

tableB:

BudgetCode Budget
271098     20000

I want to sum Amount in tableA and subtract it from Budget value 20000

20000 - 9000 = 11000
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nas
  • 1
  • 1
  • this is my sql statement: select (SELECT Budget AS Total1 from tableB) - (SELCT SUM(amount) AS Total2 From tableA) AS Result – Nas Feb 24 '16 at 18:21
  • Don't try to put code in a comment. Edit your post instead. – Tab Alleman Feb 24 '16 at 18:23
  • What's the problem you're having? Your SQL query seems to work fine, except for the typo `SELCT` (but as @TabAlleman said, it should be edited into your post, not posted as a comment). – Dan Field Feb 24 '16 at 18:33
  • 1
    Have you taken a look at http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables – KornMuffin Feb 24 '16 at 18:33

2 Answers2

0

You have to use:

 SELECT (SELECT [Budget] FROM tableB) - (SELECT SUM([amount]) FROM tableA) AS [any_name]
Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
0

This will work if TableB has just one row forever, but it's likely your requirements go beyond that:

SELECT tableB.Budget - (SELECT SUM(amount) FROM tableA)
FROM tableB.Budget;
Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69