Pergunta

I'm trying to build the following query with Ormlite:

SELECT Column1,
      COUNT(Column2),SUM(Column2)

  FROM Table

  WHERE Column3 = 1

  GROUP BY Column1;

I'm using QueryBuilder for this, but I can't figure out to get the sum and still have one whole list as a result.

What type would this result list be? I can't make it List since sum and count are not columns in the table.

Foi útil?

Solução 2

I'm using QueryBuilder for this, but I can't figure out to get the sum and still have one whole list as a result.

Once you use COUNT or SUM then you should use a raw query instead of a query that returns entities. With a raw query you can either get the string result columns directly or map the selected columns and values using a row mapper.

See the docs here:

http://ormlite.com/docs/raw-queries

Outras dicas

I'm using this way

QueryBuilder<UsageStats, Integer> b = dao.queryBuilder();
b.selectRaw("SUM(" + UsageStats.COLUMN_VALUE + ")");
b.groupBy(UsageStats.COLUMN_TYPE);
b.where().eq(UsageStats.COLUMN_TYPE, type.toString());
dao.queryRawValue(b.prepareStatementString());
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top