如何按日期获取 SUM 而不会丢失依赖于求和实例的计算
How do I get SUM by date without losing a calculation that relies on summing instance
代码:
SELECT occurred_at,
SUM(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) AS has_create,
SUM(CASE WHEN activity_name = 'Resolve' THEN 1 ELSE 0 END) AS has_resolve
FROM Activity_Data_Table$
WHERE (occurred_at > CURRENT_TIMESTAMP - 31)
AND (activity_name IN ('create', 'Resolve'))
GROUP BY session_id, occurred_at
HAVING (MAX(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) <> 0)
尝试确定每天在同一会话中创建和解决的工单百分比。工单在创建之前无法解决。时间无关紧要,前提是它们在同一 'session' 中。票证绝对可以在一个会话中创建并在另一个会话中解决。还有其他状态,它们无关紧要。不关心是否解决了某些问题,而是来自不同的会话。因此,完成的结果将包含一个额外的列,其中包含以下数据的百分比 has_resolve/has_create 在每日级别。
上面的当前输出:
像这样:
WITH dat
AS
(
SELECT occurred_at,
SUM(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) AS has_create,
SUM(CASE WHEN activity_name = 'Resolve' THEN 1 ELSE 0 END) AS has_resolve
FROM Activity_Data_Table$
WHERE (occurred_at > CURRENT_TIMESTAMP - 31)
AND (activity_name IN ('create', 'Resolve'))
GROUP BY session_id, occurred_at
HAVING (MAX(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) <> 0)
)
SELECT occurred_at, SUM(CASE WHEN has_create + has_resolve = 2 THEN 1 ELSE 0 END) * 1.0 / COUNT(*) AS pct_resolved_in_same_session
FROM dat
GROUP BY occurred_at;
代码:
SELECT occurred_at,
SUM(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) AS has_create,
SUM(CASE WHEN activity_name = 'Resolve' THEN 1 ELSE 0 END) AS has_resolve
FROM Activity_Data_Table$
WHERE (occurred_at > CURRENT_TIMESTAMP - 31)
AND (activity_name IN ('create', 'Resolve'))
GROUP BY session_id, occurred_at
HAVING (MAX(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) <> 0)
尝试确定每天在同一会话中创建和解决的工单百分比。工单在创建之前无法解决。时间无关紧要,前提是它们在同一 'session' 中。票证绝对可以在一个会话中创建并在另一个会话中解决。还有其他状态,它们无关紧要。不关心是否解决了某些问题,而是来自不同的会话。因此,完成的结果将包含一个额外的列,其中包含以下数据的百分比 has_resolve/has_create 在每日级别。
上面的当前输出:
像这样:
WITH dat
AS
(
SELECT occurred_at,
SUM(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) AS has_create,
SUM(CASE WHEN activity_name = 'Resolve' THEN 1 ELSE 0 END) AS has_resolve
FROM Activity_Data_Table$
WHERE (occurred_at > CURRENT_TIMESTAMP - 31)
AND (activity_name IN ('create', 'Resolve'))
GROUP BY session_id, occurred_at
HAVING (MAX(CASE WHEN activity_name = 'create' THEN 1 ELSE 0 END) <> 0)
)
SELECT occurred_at, SUM(CASE WHEN has_create + has_resolve = 2 THEN 1 ELSE 0 END) * 1.0 / COUNT(*) AS pct_resolved_in_same_session
FROM dat
GROUP BY occurred_at;