【发布时间】:2020-12-29 08:46:37
【问题描述】:
我有如下数据
Number ActivityID UnitID Description Date
1231 3456 334 Issue 2020-12-01 23:02:44
1231 3457 334 Restarted 2020-12-01 23:45:44
1231 3458 334 closed till 31-12-9999 2020-12-01 23:46:32
1232 4532 445 Issue 2020-12-01 23:45:44
1232 4533 445 closed till 31-12-9999 2020-12-01 23:67:44
Number 是票号,Activity 是票号中所有活动的唯一 ID,UnitId 是产品的编号,action 是对解决问题的描述和日期。
当一个工单活动被重新启动然后关闭到 31-12-9999 时,这意味着它在 App-A 中被重新启动。
如果工单仅“关闭至 31-12-9999”且之前没有重新启动活动,则表示它已在 app-B 中重新启动。
我的预期结果:
month AppA AppB
Dec 2020 1 1
我试图用下面的查询来计算它们的数量,
select Format(Date, 'MMMM-yyyy') as Month, sum(AppA) as AppA, sum(AppB) as AppB
from (
select Date, Description,
case when lower(Description) like '%31-12-9999%' then 1 else 0 end as AppB,
case when lower(Description) like '%Restarted%' then 1 else 0 end as AppA
from fct.SurveillanceAction
) a
group by Format(Date, 'MMMM-yyyy')
我不知道如何在逻辑上编写 AppA 计数(当工单活动重新启动然后关闭到 31-12-9999 然后 1)
条件是:
- App A 之前只有 31-12-9999 并且没有“重新启动”消息。它应该只计算那些没有重新启动但有 31-12-9999 的人。
- AppB 具有“31-12-9999”和重新启动消息,只有在两者连续时才应考虑。
谁能帮帮我?
【问题讨论】:
-
“月”基于什么日期?
-
@GordonLinoff,日期字段。即活动发生的时间。
标签: sql sql-server ssms