0

I have a MySQL query that's using SUM() to take the sum of a column, BUT what I need it to do is basically this:

If the string in another column is 'BUY', then I want to add the number in my column, but if this other column has 'SEL', I want to subtract the number in my column. I'm not sure how to go about this.

Any help ASAP would be appreciated. Thanks. :confused:

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by roachae
0

Use an IF clause, like in

select sum(amount * if(ordertype='BUY',1,-1)) from mytable

If this does not suit your needs, submit a test case.

0

I have a MySQL query that's using SUM() to take the sum of a column, BUT what I need it to do is basically this:

If the string in another column is 'BUY', then I want to add the number in my column, but if this other column has 'SEL', I want to subtract the number in my column. I'm not sure how to go about this.

Any help ASAP would be appreciated. Thanks. :confused:

Nevermind. What I needed was slightly different and was even a simpler fix.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.