表格'volcado':id,reserva,candidad
用以下查询:
SELECT (CASE
WHEN sum(cantidad) > 0 THEN 1
WHEN sum(cantidad) <= 0 THEN 0
END) AS suma
FROM volcado
GROUP BY reserva
结果是:
╔══════╗
║ suma ║
╠══════╣
║ 1 ║
║ 1 ║
║ 0 ║
║ 1 ║
║ 0 ║
╚══════╝
我需要查询的结果将是:3
如何在同一查询中对Suma的行进行SUM()?
我不使用它运行:
SELECT SUM(SELECT (CASE
WHEN sum(cantidad) > 0 THEN 1
WHEN sum(cantidad) <= 0 THEN 0
END) AS suma FROM volcado GROUP BY reserva)
FROM volcado
错误:
子查询返回超过1行
提前致谢。
最佳答案
试试这个
SELECT SUM(a.suma) from
(SELECT (CASE
WHEN sum(cantidad) > 0 THEN 1
WHEN sum(cantidad) <= 0 THEN 0
END) AS suma FROM volcado GROUP BY reserva)a
关于mysql - SUM()GROUP BY列的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36887032/