SQL返回有总和值

时间:2017-02-16 09:56:00

标签: mysql sql

我有一个检查组的查询,并确保它在2

下有超过1的值
SELECT `tile` FROM TFResults
GROUP BY `tile`
HAVING SUM(CASE WHEN `Place` < 2 THEN 1 ELSE 0 END)> 1 ;

我想要返回sum的值,但似乎无法让它工作

SELECT `tile`, thesum
FROM TFResults
GROUP BY `tile`
HAVING SUM(CASE WHEN `Place` < 2 THEN 1 ELSE 0 END) as thesum > 1 ;

3 个答案:

答案 0 :(得分:1)

您可以在select子句

中定义别名
SELECT tile, 
       SUM(CASE WHEN Place < 2 THEN 1 ELSE 0 END) as thesum
FROM TFResults
GROUP BY tile
HAVING thesum > 1

答案 1 :(得分:0)

sum语句移至select

SELECT
    `tile`,
    SUM(CASE WHEN `Place` < 2 THEN 1 ELSE 0 END) AS thesum 
FROM TFResults
GROUP BY `tile`
HAVING thesum > 1 ;

答案 2 :(得分:0)

首先,您需要将sum部分移动到select语句。如果您只需要检查一列,则使用if if CASE。检查下面

SELECT tile, SUM(if(Place < 2, 1, 0)) place_sum 
FROM TFResults
GROUP BY tile
HAVING place_sum > 1