包含将其他Concurrent和Active票据计为新列的函数的SQL查询

时间:2017-03-09 02:01:19

标签: sql sql-server

您好我想查询一个SQL dbo,以便在创建当前行“ticket”的20分钟窗口内获取打开(启动后和预先解析)的“门票”数量。

目标表(其中4项在8:50到9:10之间有效):

ID   CreateDate         ConcurrentItems
123  12/1/2016 09:00        4

有些事情:

SELECT   Ticket_ID as ID, Created_Date as CreateDate,
    Count( 
            WHERE
             WorkItemType = 'Work Request' AND
             IncidentStartTime <= (Created_Date minus 10 minutes) AND
             IncidentResolvedTime > (Created_Date plus 10 minutes)
     )as ConcurrentItems

FROM   CurrentView

COUNT中的Created_Date与SELECT

中的相同

2 个答案:

答案 0 :(得分:0)

我建议outer apply

SELECT Ticket_ID as ID, Created_Date as CreateDate, x.ConcurrentItems
FROM CurrentView v OUTER APPLY
     (SELECT COUNT(*) as ConcurrentItems
      FROM CurrentView v2
      WHERE v2.WorkItemType = 'Work Request' AND
            v2.IncidentStartTime <= DATEADD(minute, -10, v.Created_Date) AND 
            v2.IncidentResolvedTime > DATEADD(minute, 10, Created_Date)
    ) x;

答案 1 :(得分:0)

这将显示每个具有条件的CreatedDate的计数:

select Created_Date, count(1) as ConcurrentItems from CurrentView
where WorkItemType = 'Work Request' AND
IncidentStartTime <= DATEADD(Minute, -10 CreateDate) AND
IncidentResolvedTime > DATEADD(Minute, +10 CreateDate) group by Created_Date

但是这将显示每个CreatedDate的计数。