根据案例 when-then 在 mysql 中设置一个变量
set a variable in mysql based on case when-then
我有一个项目 table,包含三列:Task_ID、Start_Date和 End_Date。
每当前一行的 End_Date 和当前行的 [ 之间的差异时,我正在尝试两次递增一个名为 count =36=] 不等于零,但每一行都会递增。那么,如何根据 CASE when-then 语句更改 mysql 中变量的值?
SQL 查询 -
SET @count := 0;
SELECT
*,
LAG(end_date) OVER(ORDER BY start_date) as prev,
DATEDIFF(LAG(end_date) OVER(ORDER BY start_date), start_date) as diff,
(CASE
WHEN DATEDIFF(LAG(end_date) OVER(ORDER BY start_date), start_date) is NULL
THEN @count
WHEN DATEDIFF(LAG(end_date) OVER(ORDER BY start_date), start_date) = 0
THEN @count
ELSE @count := @count + 1
END) as cnt
FROM projects
;
输出-
task_id start_date end_date prev diff cnt
1 2015-10-01 2015-10-02 NULL NULL 1
24 2015-10-02 2015-10-03 2015-10-02 0 2
2 2015-10-03 2015-10-04 2015-10-03 0 3
23 2015-10-04 2015-10-05 2015-10-04 0 4
3 2015-10-11 2015-10-12 2015-10-05 -6 5
22 2015-10-12 2015-10-13 2015-10-12 0 6
4 2015-10-15 2015-10-16 2015-10-13 -2 7
21 2015-10-17 2015-10-18 2015-10-16 -1 8
5 2015-10-19 2015-10-20 2015-10-18 -1 9
预期输出 -
task_id start_date end_date prev diff cnt
1 2015-10-01 2015-10-02 NULL NULL 0
24 2015-10-02 2015-10-03 2015-10-02 0 0
2 2015-10-03 2015-10-04 2015-10-03 0 0
23 2015-10-04 2015-10-05 2015-10-04 0 0
3 2015-10-11 2015-10-12 2015-10-05 -6 1
22 2015-10-12 2015-10-13 2015-10-12 0 1
4 2015-10-15 2015-10-16 2015-10-13 -2 2
21 2015-10-17 2015-10-18 2015-10-16 -1 3
5 2015-10-19 2015-10-20 2015-10-18 -1 4
你不知道。使用 window 函数:
SELECT p.*,
SUM(diff < 0) OVER (ORDER BY start_date)
FROM (SELECT p.*,
LAG(end_date) OVER(ORDER BY start_date) as prev,
DATEDIFF(LAG(end_date) OVER (ORDER BY start_date), start_date) as diff
FROM projects p
) p;
对于 SUM(CASE WHEN diff < 0 THEN 1 ELSE 0 END)
,SUM(diff < 0)
是 shorthand。 MySQL 在算术上下文中将布尔值视为整数。
我有一个项目 table,包含三列:Task_ID、Start_Date和 End_Date。 每当前一行的 End_Date 和当前行的 [ 之间的差异时,我正在尝试两次递增一个名为 count =36=] 不等于零,但每一行都会递增。那么,如何根据 CASE when-then 语句更改 mysql 中变量的值?
SQL 查询 -
SET @count := 0;
SELECT
*,
LAG(end_date) OVER(ORDER BY start_date) as prev,
DATEDIFF(LAG(end_date) OVER(ORDER BY start_date), start_date) as diff,
(CASE
WHEN DATEDIFF(LAG(end_date) OVER(ORDER BY start_date), start_date) is NULL
THEN @count
WHEN DATEDIFF(LAG(end_date) OVER(ORDER BY start_date), start_date) = 0
THEN @count
ELSE @count := @count + 1
END) as cnt
FROM projects
;
输出-
task_id start_date end_date prev diff cnt
1 2015-10-01 2015-10-02 NULL NULL 1
24 2015-10-02 2015-10-03 2015-10-02 0 2
2 2015-10-03 2015-10-04 2015-10-03 0 3
23 2015-10-04 2015-10-05 2015-10-04 0 4
3 2015-10-11 2015-10-12 2015-10-05 -6 5
22 2015-10-12 2015-10-13 2015-10-12 0 6
4 2015-10-15 2015-10-16 2015-10-13 -2 7
21 2015-10-17 2015-10-18 2015-10-16 -1 8
5 2015-10-19 2015-10-20 2015-10-18 -1 9
预期输出 -
task_id start_date end_date prev diff cnt
1 2015-10-01 2015-10-02 NULL NULL 0
24 2015-10-02 2015-10-03 2015-10-02 0 0
2 2015-10-03 2015-10-04 2015-10-03 0 0
23 2015-10-04 2015-10-05 2015-10-04 0 0
3 2015-10-11 2015-10-12 2015-10-05 -6 1
22 2015-10-12 2015-10-13 2015-10-12 0 1
4 2015-10-15 2015-10-16 2015-10-13 -2 2
21 2015-10-17 2015-10-18 2015-10-16 -1 3
5 2015-10-19 2015-10-20 2015-10-18 -1 4
你不知道。使用 window 函数:
SELECT p.*,
SUM(diff < 0) OVER (ORDER BY start_date)
FROM (SELECT p.*,
LAG(end_date) OVER(ORDER BY start_date) as prev,
DATEDIFF(LAG(end_date) OVER (ORDER BY start_date), start_date) as diff
FROM projects p
) p;
对于 SUM(CASE WHEN diff < 0 THEN 1 ELSE 0 END)
,SUM(diff < 0)
是 shorthand。 MySQL 在算术上下文中将布尔值视为整数。