根据累计 运行 总数分配标志
Assign flag based on cumulative running total
我有一个 table,如上所示,它来自以下查询:
SELECT f_id, f_datetime, (f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs,
IF(DATE(f_datetime) = DATE(NOW()), 1, 0) AS f_status
FROM results
我希望我创建的 f_status 查询列具有以下规则:
1 - If `DATE(f_datetime) = DATE(NOW())` THEN assign `f_status=1`, which is what I've done.
2 - take the maximum total legs where `f_status=1` (i.e. 4 and add 50), then assign `f_status=2` up to total_legs=54,
3) Anything after 2) assign as 3
下面的期望输出:
我知道怎么做,但它需要很多 table 别名,而且真的很乱。有简单的方法吗?
谢谢
如果我没看错的话,你可以添加一个 CASE
表达式和你想要的布尔逻辑:
SELECT f_id, f_datetime,
(f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs,
(CASE WHEN DATE(f_datetime) = CURDATE())
THEN 1
WHEN SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) <= 54
THEN 2
ELSE 3
END) AS f_status
FROM results ;
编辑:
如果54
只是50
加上当天,你可以使用:
SELECT f_id, f_datetime,
(f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score + f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs,
(CASE WHEN DATE(f_datetime) = CURDATE()
THEN 1
WHEN SUM(f_player1_score + f_player2_score) OVER (ORDER BY f_datetime) <=
50 + SUM(CASE WHEN DATE(f_datetime) = CURDATE() THEN f_player1_score + f_player2_score ELSE 0 END)
THEN 2
ELSE 3
END) AS f_status
FROM results ;
感谢使用 CASE
表达式的建议...
SELECT X.*,
(CASE WHEN DATE(f_datetime) = CURDATE() then 1
WHEN total_legs<= MAX(CASE WHEN DATE(f_datetime) = CURDATE() THEN total_legs + 50 END) OVER(ORDER BY f_datetime DESC) then 2
ELSE 3 END) AS f_status
FROM
(
SELECT f_id, f_datetime, (f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs
FROM results
) X
我有一个 table,如上所示,它来自以下查询:
SELECT f_id, f_datetime, (f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs,
IF(DATE(f_datetime) = DATE(NOW()), 1, 0) AS f_status
FROM results
我希望我创建的 f_status 查询列具有以下规则:
1 - If `DATE(f_datetime) = DATE(NOW())` THEN assign `f_status=1`, which is what I've done.
2 - take the maximum total legs where `f_status=1` (i.e. 4 and add 50), then assign `f_status=2` up to total_legs=54,
3) Anything after 2) assign as 3
下面的期望输出:
我知道怎么做,但它需要很多 table 别名,而且真的很乱。有简单的方法吗?
谢谢
如果我没看错的话,你可以添加一个 CASE
表达式和你想要的布尔逻辑:
SELECT f_id, f_datetime,
(f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs,
(CASE WHEN DATE(f_datetime) = CURDATE())
THEN 1
WHEN SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) <= 54
THEN 2
ELSE 3
END) AS f_status
FROM results ;
编辑:
如果54
只是50
加上当天,你可以使用:
SELECT f_id, f_datetime,
(f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score + f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs,
(CASE WHEN DATE(f_datetime) = CURDATE()
THEN 1
WHEN SUM(f_player1_score + f_player2_score) OVER (ORDER BY f_datetime) <=
50 + SUM(CASE WHEN DATE(f_datetime) = CURDATE() THEN f_player1_score + f_player2_score ELSE 0 END)
THEN 2
ELSE 3
END) AS f_status
FROM results ;
感谢使用 CASE
表达式的建议...
SELECT X.*,
(CASE WHEN DATE(f_datetime) = CURDATE() then 1
WHEN total_legs<= MAX(CASE WHEN DATE(f_datetime) = CURDATE() THEN total_legs + 50 END) OVER(ORDER BY f_datetime DESC) then 2
ELSE 3 END) AS f_status
FROM
(
SELECT f_id, f_datetime, (f_player1_score+f_player2_score) AS legs,
SUM(f_player1_score+f_player2_score) OVER (ORDER BY f_datetime DESC) AS total_legs
FROM results
) X