I need some help with the SUM feature in android app. I have a table that looks something like the following :
I have need to SUM
Quantities between last two records Notes and last one record with Note. I need to sum Quantity of rows 31,32 and 33. It would return 90. I've tried
SELECT Sum(QUANTITY) FROM fuel_table WHERE NOTE!='' ORDER BY ID DESC
but it returns SUM of all quantities with note.
The SQL Server SUM() function is an aggregate function that calculates the sum of all or distinct values in an expression. The syntax of the SUM() function is as follows: SUM([ALL | DISTINCT ] expression) In this syntax: ALL instructs the SUM() function to return the sum of all values including duplicates.
If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; The SELECT statement in SQL tells the computer to get data from the table.
In SQLite SUM() Function is an aggregate function which is used to calculate the sum of values in a specified expression or column. Generally, in SQLite SUM() function will work with non-NULL numeric values to return the summed values of a column in a table.
To sum rows with same ID, use the GROUP BY HAVING clause.
I am inclined to phrase the question as: sum the quantity from all rows that have one note "ahead" of them. This suggests:
select sum(quantity)
from (select ft.*,
(select count(*)
from fuel_table ft2
where ft2.note = 'Yes' and ft2.id >= ft.id
) as numNotesAhead
from fuel_table ft
) ft
where numNotesAhead = 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With