Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql subtract value...

Status
Not open for further replies.

burnside

Technical User
Dec 4, 2004
236
GB
Hi,
i want to subtract `spend` from `target` as `diff`

[target]-[spend] AS diff

but i get error on above line
have the following statement;

strsql = "SELECT tbl_item.itemid, tbl_item.item, budgmonid, budgyearid, SUM(budgexp) as spend, SUM(budgtar) as target, tbl_loc.locname, tbl_fran.fran FROM tbl_budg INNER JOIN tbl_item ON tbl_budg.itemid = tbl_item.itemid, [target]-[spend] AS diff INNER JOIN tbl_fran ON tbl_item.franid = tbl_fran.franid INNER JOIN tbl_loc ON tbl_item.siteid = tbl_loc.locid WHERE budgyearid = 2 GROUP BY tbl_item.itemid, item, budgmonid, budgyearid ORDER BY itemid, budgyearid, budgmonid
 
strsql = "SELECT tbl_item.itemid, tbl_item.item, budgmonid, budgyearid, Sum(budgexp) AS spend, Sum(budgtar) AS target, Sum(budgtar)-SUM(budgexp) AS diff, tbl_loc.locname, tbl_fran.fran FROM ((tbl_budg INNER JOIN tbl_item ON tbl_budg.itemid = tbl_item.itemid) INNER JOIN tbl_fran ON tbl_item.franid = tbl_fran.franid) INNER JOIN tbl_loc ON tbl_item.siteid = tbl_loc.locid WHERE budgyearid = 2 GROUP BY tbl_item.itemid, tbl_item.item, budgmonid, budgyearid,tbl_loc.locname, tbl_fran.fran ORDER BY itemid, budgyearid, budgmonid"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Your problem may be that [target] and [spend] are themselves calculated fields. Try using:
Code:
SUM(budgtar) - SUM(budgexp) AS diff
... so that 'diff' is calculated directly from the 'budgexp' and 'budgetar' columns.

I hope that this helps.


Bob Stubbs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top