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

Page 1 of 1

## 7 Replies - 20853 Views - Last Post: 10 April 2009 - 08:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=96064&amp;s=2401a7d82e85c9d21a8db948287fe84e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 erus

• New D.I.C Head

Reputation: 0
• 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

• Your worst nightmare

Reputation: 34
• 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

• New D.I.C Head

Reputation: 0
• 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

• New D.I.C Head

Reputation: 0
• 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

• New D.I.C Head

Reputation: 3
• 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

• MSX

Reputation: 131
• 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

• Dreaming Coder

Reputation: 6129
• Posts: 13,237
• 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

• New D.I.C Head

Reputation: 0
• 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

baavgai, 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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }