2

Ok so I have a mySQL table of values. Some are positive, some are negative. The negative ones have a - infront of them in the table. I'm using Codeigniter.

I need to sum them all together, but IGNORE the - before the negative values. I just want to sum the numbers, not sum them as negative numbers.

So for example, this is what it does currently: -55 + -20 = 35

But what I want it to do is: -55 + -20 = 75

Basically I want to sum the values only, regardless of their positive or negative type.

How can I do this? Here's my query:

$this->db->select_sum('vat')
     ->from('accounts')->where_in('type', 'Expenses')
     ->where('date <=', $current_period)
     ->where('date >=', $previous_period);
1
  • 5
    you can use "sum(abs(<column name>))"
    – jsist
    Aug 6, 2012 at 13:26

4 Answers 4

5

Perhaps something like this will work:

$this->db->select('SUM(CASE WHEN vat >= 0 THEN vat ELSE -vat END) AS sum', false)->from('accounts')->where_in('type', 'Expenses')->where('date <=', $current_period)->where('date >=', $previous_period);

This should keep positive numbers in vat positive and convert negative numbers to positive. Note: the false in the select() statement stops CI from auto-escaping the fields.

Also, as picked up from a comment from @shubhansh, you can use MySQL's ABS() method to get the absolute-value instead of the CASE:

$this->db->select('SUM(ABS(vat)) AS sum', false)->from('accounts')->where_in('type', 'Expenses')->where('date <=', $current_period)->where('date >=', $previous_period);
0
0

I can think of two ways.

First way: I'm pretty sure CI escapes field names, so to do it this way you need to execute normal query:

$this->db->query('SELECT SUM(AVG(vat)) FROM accounts WHERE ...'); // Replace ... with WHERE parameters 

Second way: Or do two queries and sum the results:

$positive = $this->db->select_sum('vat')->from('accounts')->where_in('type', 'Expenses')->where('date <=', $current_period)->where('date >=', $previous_period)->where('vat >=', 0);
$negative = $this->db->select_sum('vat')->from('accounts')->where_in('type', 'Expenses')->where('date <=', $current_period)->where('date >=', $previous_period)->where('vat <', 0);
$result = $positive + abs($negative);

Mind that second option would take longer time to complete as those are two separate queries.

0

I had the same issue doing a date search on a db level - best I could do was use ABS() since time_diff returns a negative value if the result is in the past. If are working with date_diff or TIME_DIFF use this:

TIME(ABS(TIMEDIFF(TIME(sch_Starts), TIME("22:00:00"))))

it takes the possibly negative result, converts it via ABS and wrap the TIME() around that to convert back to time if u are using time specific functions like me.

Tedious perhaps but it works 100%

-2

You could always try:

where table.field NOT LIKE '%-%'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.