7 Replies - 19071 Views - Last Post: 10 April 2009 - 08:27 AM Rate Topic: -----

#1 erus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-October 06

how to subtract two specific values from a column in a table

Posted 29 March 2009 - 01:44 PM

hi guys;i m about to flip out.Here is my problem;
i want to extract two specific values from a column,to illustrate simply;

name value
a 10
b 20
c 30
d 40

what i wanna do is just to perform this function;
a+b-(c+d)
i know it seems so simple but i haven't make it work yet. thanks for your help by now

This post has been edited by erus: 29 March 2009 - 01:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: how to subtract two specific values from a column in a table

#2 P4L  Icon User is offline

  • Your worst nightmare
  • member icon

Reputation: 34
  • View blog
  • Posts: 2,788
  • Joined: 07-February 08

Re: how to subtract two specific values from a column in a table

Posted 29 March 2009 - 03:03 PM

This should help.

Select *
From Table
Where a+b-(c+d) as Value


Was This Post Helpful? 0
  • +
  • -

#3 erus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-October 06

Re: how to subtract two specific values from a column in a table

Posted 29 March 2009 - 11:48 PM

thx for try but its not so simple:)
you didn't indicate where a,b,c,d come from.let me explain my tought;
select sum(value) from table1 where name="a" or name="b"-select sum(value) from table1 where name="c" or name="d"

but it doesnt work..

This post has been edited by erus: 29 March 2009 - 11:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 erus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-October 06

Re: how to subtract two specific values from a column in a table

Posted 31 March 2009 - 12:54 PM

i m waiting for your remarks..
it is not so hard

This post has been edited by erus: 31 March 2009 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#5 cardographer  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 15
  • Joined: 24-March 09

Re: how to subtract two specific values from a column in a table

Posted 31 March 2009 - 08:44 PM

Something like...

select a.value+b.value-(c.value+d.value)
from tbl a, tbl b, tbl c, tbl d
where a.name = 'a' and b.name = 'b' and c.name = 'c' and d.name = 'd'

Was This Post Helpful? 0
  • +
  • -

#6 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: how to subtract two specific values from a column in a table

Posted 02 April 2009 - 04:45 PM

hello,
you can use sub queries for this
assuming your table called tbl
select
(select value from tbl where name='a')+(select value from tbl where name='b')
-
(
(select value from tbl where name='c')+(select value from tbl where name='d')
)



hope it helps
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5943
  • View blog
  • Posts: 12,871
  • Joined: 16-October 07

Re: how to subtract two specific values from a column in a table

Posted 02 April 2009 - 06:38 PM

Not the ideal problem for a database. Still, in SQL, I'd probably do this:
select 
	sum(case when name='a' then value else 0 end)
	+ sum(case when name='b' then value else 0 end)
	- ( 
		sum(case when name='c' then value else 0 end)
		+ sum(case when name='d' then value else 0 end) 
	) as result
	from table
	where name in ('a','b','c','d')


This post has been edited by baavgai: 02 April 2009 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

#8 erus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-October 06

Re: how to subtract two specific values from a column in a table

Posted 10 April 2009 - 08:27 AM

View Postbaavgai, on 2 Apr, 2009 - 05:38 PM, said:

Not the ideal problem for a database. Still, in SQL, I'd probably do this:
select 
	sum(case when name='a' then value else 0 end)
	+ sum(case when name='b' then value else 0 end)
	- ( 
		sum(case when name='c' then value else 0 end)
		+ sum(case when name='d' then value else 0 end) 
	) as result
	from table
	where name in ('a','b','c','d')


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1