[英]sql query SUM IF GROUP BY
This is my table expense: 这是我的餐桌费:
id2 name exp date ...
1 Jack 2
2 Joe 3.1
3 Marian 5.8
1 Jack 2
1 Jack 4
3 Marian 3
I want this query: sum up all person expenses and if the expenses was more than 5 echo it. 我想要这个查询:总结所有人的支出,如果支出超过5,则回显它。
I have a problem on if section (if more than 5). 我对if部分有疑问(如果大于5)。 I grouped by id2 and
SUM(exp)
and works fine but I don't know how to imply IF. 我按id2和
SUM(exp)
分组,工作正常,但我不知道如何暗示IF。 I wrote SUM(IF(exp >= 5, 1, 0))
but sql error says is not correct group function. 我写了
SUM(IF(exp >= 5, 1, 0))
但是sql错误说是不正确的组函数。
S`ELECT id2, SUM(IF(exp >= 5, 1, 0)) as ex FROM expense GROUP BY id2`
I appreciate if you can help me with the query. 谢谢您的查询。 Thanks
谢谢
It is possible that this is what you want: 这可能是您想要的:
SELECT id2, SUM(exp),
GROUP_CONCAT(CASE WHEN exp > 5 THEN exp END) as ExpsGreaterThan5
FROM expense
GROUP BY id2;
select id2,name,sum(exp) from expense group by user_id having sum(exp)>5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.