Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in getting Sum

I have this data:

name    qty     date                    flag  
---------------------------------------------    
abc     255     11/10/1986 12:00:00 AM  IN
abc     300     11/10/2010 12:00:00 AM  IN
abc     12      11/10/2012 12:00:00 AM  OUT
abc     13      11/9/2010 12:00:00 AM   OUT
NULL    NULL    NULL                    NULL

I want to get sum of qty at that specific row:

  • If flag is "in", then it will add to the sum
  • if flag is "out", then it will subtract from the sum
like image 966
Rohan Avatar asked Sep 29 '10 08:09

Rohan


People also ask

Why is the sum function not working?

The most common reason for AutoSum not working in Excel is numbers formatted as text. At first sight, those values may look like normal numbers, but Excel regards them as text strings and does not include in calculations.

Why I am not getting the sum in Excel?

Check for Automatic Recalculation On the Formulas ribbon, look to the far right and click Calculation Options. On the dropdown list, verify that Automatic is selected. When this option is set to automatic, Excel recalculates the spreadsheet's formulas whenever you change a cell value.

How do I fix the sum error in Excel?

The syntax of the array formula will be =SUM(IFERROR(A1:A8,0)). In this case, the IFERROR function is evaluating the contents of cells A1:A8 and, if there is an error, replacing the error value with zero. The SUM function is then used to sum the values. Enter this function is cell A11.


1 Answers

SELECT SUM(case flag when 'IN' then qty else -qty end)
from table
WHERE ..... Your conditions here ...
like image 166
Michael Pakhantsov Avatar answered Nov 04 '22 07:11

Michael Pakhantsov