将两列的总和与一列进行比较

Compare sum of two columns to one column

我正在尝试将两列值的总和与第三列值进行比较,然后将字符串文字显示为结果集值。这是我的查询

这是我的示例数据架构

Player_Games
------------------------
Game1 | Game 2 | Game 3
------------------------
 20   |   13   |    45
------------------------
 14   |   27   |    25
------------------------
 18   |   17   |    36
------------------------
 20   |   20   |    29
------------------------
 32   |   10   |    33
------------------------

SELECT 
CASE
    WHEN((
        SELECT SUM(Game1 + Game2) as total FROM Player_Games
        ) 
        < 
        (
        SELECT Game3 FROM Player_Games
        ))
         THEN "Expected_Performance"
END as Result

来自 Player_Games;

预期结果

Expected Performance
NULL
Expected Performance
NULL
NULL

但是,抛出一个错误ERROR 1242 (21000) at line 4: Subquery returns more than 1 row

我在这里错过了什么?我需要GROUP BY Game3?

这里不需要阳光。只是案例:

    SELECT 
    CASE
        WHEN Game1 + Game2 < Game3
             THEN 'Expected_Performance'
    END as Result
FROM Player_Games

您可以使用 CASE 表达式检查 game1 和 game2 的总和是否小于 game3 然后添加所需的文本。

查询

select 
case when game1 + game2 < game3
   then 'Expected performance' 
   else null end as result
from player_games;
SELECT CASE WHEN t1.Game3 > t2.total THEN "Expected_Performance" END
FROM Player_Games t1
CROSS JOIN (SELECT SUM(Game1 + Game2) as total FROM Player_Games) t2

8+

SELECT CASE WHEN Game3 > SUM(Game1 + Game2) OVER () THEN "Expected_Performance" END
FROM Player_Games

如果您需要汇总的不是整个 table,而是单行,那么

SELECT CASE WHEN Game3 > Game1 + Game2 THEN "Expected_Performance" END
FROM Player_Games

PS。不能保证输出行顺序与源行顺序匹配(在所有查询中)。因此,您必须在输出列表中添加一些列来标识单独的行。