0

I need to get sum of the value field from Expense object and display this in textview, but I really don't know how to do it in Room Database. I need to create specific query in Dao or there is some other way?

Pojo:

@Entity(tableName = "expense_table")
public class Expense {

@PrimaryKey
private int id;
private String note;
private Double value;
private String type;

Dao:

@Dao
public interface ExpenseDao {

@Insert
void insertExpense(Expense expense);

@Query("SELECT * FROM expense_table")
LiveData<List<Expense>> getExpensesByDay();
beginner992
  • 659
  • 1
  • 9
  • 28

1 Answers1

3

You need to create a new query in DAO

@Query("SELECT COALESCE(sum(COALESCE(value,0)), 0) From expense_table")
LiveData<Double> getTotal();
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46