您可以在一个查询中进行多个查询。我有一个相同的表,不同的地方和多个选择总和: 错误的代码:
public Cursor listado_resumen(String s_mes, String s_ano) {
String s_actual = "-" + s_mes + "-" + s_ano;
String s_actual_ano = "-" + s_ano;
String lista_re = "SELECT *, (SELECT SUM (tr_euros) FROM bdt_registro WHERE tr_fecha LIKE '_%" + s_actual_ano + "' GROUP BY tr_codigo ORDER BY tr_codigo) AS total, (SELECT SUM (tr_euros) FROM bdt_registro WHERE tr_fecha LIKE '_%" + s_actual + "' GROUP BY tr_codigo ORDER BY tr_codigo) AS actual FROM bdt_registro GROUP BY tr_codigo ORDER BY tr_codigo";
return db.rawQuery(lista_re, null);
}
返回:
只有第一个值
cod。|| descrip || total || actual(month)
a || house || 100 || 100
b || car || 100 || 100
c ||食物|| 100 || 100
我喜欢回报:
cod。|| descrip || total || actual(month)
a || house || 100 || 20个
b || car || 20 || 5
c ||食物|| 5 || 0
感谢您的关注
答案 0 :(得分:0)
标量子查询必须只返回一个值。 (SQLite忽略任何其他值;其他数据库可能会报告错误。)
您必须编写子查询,以便它们为外部表中的相应记录返回正确的值,即它们必须是相关的子查询(我假设tr_codigo
值可用于关联) :
SELECT *,
(SELECT SUM (tr_euros)
FROM bdt_registro AS r1
WHERE tr_fecha LIKE '_%...'
AND r1.tr_codigo = bdt_registro.tr_codigo
) AS total,
(SELECT SUM (tr_euros)
FROM bdt_registro AS r2
WHERE tr_fecha LIKE '_%...'
AND r2.tr_codigo = bdt_registro.tr_codigo
) AS actual
FROM bdt_registro
GROUP BY tr_codigo
ORDER BY tr_codigo