Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

483
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda