假设我有下表:
Table A: Event Log
ID Step WeekArrived WeekCompleted
-------------------------------------------------------
1 A 1 1
1 B 1 3
2 B 3 3
所以在第1周,案例1到了。对于这种情况,步骤A在第1周完成,但步骤B不是。步骤B在第3周完成。案例2也在第3周到达,步骤B在同一周完成(本案不需要步骤A)。
我想将此事件日志聚合成每周一步的概述,如下所示:
Table B: Weekly Overview
Week Step In Backlog Out
------------------------------------------------------------------
1 A 1 0 1
1 B 1 1 0
2 A 0 0 0
2 B 0 1 0
3 A 0 0 0
3 B 1 0 2
对于每周X和步骤Y,此表显示:
如何从表A到表B?
答案 0 :(得分:0)
您需要一个周列表,以便评估该周是否有数据。
在sql server中,例如像How to generate a range of numbers between two numbers?
这样的东西然后你加入你的步骤。如果您还有一个分开的表格,那么效果会更好。
SELECT weeks.week_id, steps.step,
CASE WHEN s_in.WeekArrived IS NULL THEN 0 ELSE 1 END as step_in,
CASE WHEN s_completed.WeekCompleted IS NULL THEN 0 ELSE 1 END as step_out,
CASE WHEN s_pending.WeekArrived IS NULL THEN 0 ELSE 1 END as step_pending
FROM weeks
CROSS JOIN (SELECT DISTINCT step
FROM event_log) as steps
LEFT JOIN event_Log as s_in
ON weeeks.week_id = e.WeekArrived
AND steps.step = e.step
LEFT JOIN event_Log as s_completed
ON weeeks.week_id = e.WeekCompleted
AND steps.step = e.step
LEFT JOIN event_Log as s_pending
ON weeeks.week_id > e.WeekArrived
AND weeeks.week_id < e.WeekCompleted
AND steps.step = e.step
注意:不知道back log
是什么。
答案 1 :(得分:0)
首先,您需要一种获取week_id列表的方法,我使用了Teradata的系统日历。然后是交叉加入加上一些条件聚合:
SELECT week_id, step,
Sum(CASE WHEN week_id = WeekArrived THEN 1 ELSE 0 END) AS "in",
Sum(CASE WHEN week_id >= WeekArrived AND week_id < WeekCompleted THEN 1 ELSE 0 END) as "backlog",
Sum(CASE WHEN week_id = WeekCompleted THEN 1 ELSE 0 END) AS "out"
FROM
( -- get a lsit of week ids
SELECT day_of_calendar AS week_id
FROM sys_calendar.CALENDAR
WHERE week_id
-- calculate the range of week ids
BETWEEN (SELECT Min(WeekArrived) FROM event_log)
AND (SELECT Max(WeekCompleted) FROM event_log)
) AS weeks
CROSS JOIN
(
SELECT *
FROM event_log
) AS steps
GROUP BY week_id, step
ORDER BY week_id, step