Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

480
Visualizações
Room Database functions WEEK(), MONTH(), etc

I have a room database with a table "Entries" where I have the columns value(int), cat_id(int) and date(Date).

I want to get the sum of all entries of the current month and all entries of the current week. But the selections:

@Query("SELECT sum(value) FROM Entry WHERE cat_id = :cat_id AND WEEK(date, 5) = WEEK(:date, 5) AND YEAR(date) == YEAR(:date)")
float getValueThisWeekByCatId(int cat_id, Date date);

@Query("SELECT sum(value) FROM Entry WHERE cat_id = :cat_id AND MONTH(date) = MONTH(:date) AND YEAR(date) == YEAR(:date)")
float getValueThisMonthByCatId(int cat_id, Date date);

don't work.

This is the error:

There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such function: WEEK) float getValueThisWeekByCatId(int cat_id, Date date); Same with function MONTH

Is there another way to that selection?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

A little late but I stumbled upon this question because of a similar problem, and your solution with strftime in the answer is correct.

Tried with your table and query and it worked for me:

 CREATE TABLE Entry(
       id int primary key,
       cat_id int,
       value int,
       date date
    );

    insert into entry (id, cat_id, value, date) values (1, 1, 50, '2019-03-19');
    insert into entry (id, cat_id, value, date) values (2, 1, 30, '2019-02-20');
    
    SELECT sum(value) 
    FROM Entry 
    WHERE cat_id = 1 
    AND strftime('%m', date) = strftime('%m', '2019-03-01')
    AND strftime('%Y', date) = strftime('%Y', '2019-03-01')

Also works with CURRENTDATE but obviously have to use current date entries.

Example

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda