SQL窗口函数不能在另一个窗口函数或聚合的上下文中使用

时间:2015-03-25 09:09:49

标签: sql sql-server tsql

您有一些用于我们的呼叫记录系统的SQL,我想看看有多少呼叫违反了4小时SLQ响应时间

我遇到的问题是在第33行我使用MIN并得到错误,因为我也在使用COUNT

(Msg 4109,Level 15,State 1,Line 33 窗口函数不能在另一个窗口函数或聚合的上下文中使用。)

任何更好的编码方式或如何绕过它

    SELECT Count(CASE 
               WHEN dbo.assignment.enddate IS NOT NULL 
                    AND dbo.assignment.enddate < ( CASE 
                                                     WHEN Datepart(dw, 
       dbo.dt_list_requests.createdon) = 
       1 THEN 
       Dateadd(dd, 1, ( Dateadd(hh, 11, ( 
                   Dateadd(dd, 0, Datediff 
                   (dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 7 THEN 
       Dateadd(dd, 2, ( Dateadd(hh, 11, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6 
       AND dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( 
       Dateadd(mi, 30, ( 
       Dateadd(dd, 0, Datediff( 
       dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
         ( 
       Dateadd(hh, 11, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6 
       AND Dateadd(hh, 4, dbo.dt_list_requests.createdon) > 
       Dateadd(hh, 17, ( 
              Dateadd(mi, 30, 
              ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
         ( 
       Dateadd(hh, 17, ( Dateadd(mi, 30, 
       dbo.dt_list_requests.createdon) )) )) 
       WHEN dbo.dt_list_requests.createdon < Dateadd(hh, 7, ( 
       Dateadd(dd, 0, Datediff 
       (dd, 0, 
       dbo.dt_list_requests.createdon)) )) THEN Dateadd(hh, 11, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) 
       WHEN dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( 
           Dateadd(mi, 30, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 1, ( 
       Dateadd(hh, 11, ( 
       Dateadd(dd, 0, 
       Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Dateadd(hh, 4, dbo.dt_list_requests.createdon) > 
       Dateadd(hh, 17, ( 
       Dateadd(mi, 30, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(hh, 17, ( 
       Dateadd(mi, 30, dbo.dt_list_requests.createdon) )) 
       ELSE Dateadd(hh, 4, dbo.dt_list_requests.createdon) 
       END ) THEN NULL 
       WHEN dbo.dt_list_requests.respondedtoon IS NOT NULL 
       AND dbo.dt_list_requests.respondedtoon < ( CASE 
              WHEN Datepart(dw, 
       dbo.dt_list_requests.createdon) = 1 THEN 
       Dateadd(dd, 1, ( Dateadd(hh, 11, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, 
       dbo.dt_list_requests.createdon) = 7 THEN 
       Dateadd(dd, 2, ( Dateadd(hh, 11, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, 
       dbo.dt_list_requests.createdon) = 6 
       AND 
       dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( 
       Dateadd(mi, 30, ( 
       Dateadd(dd, 0, Datediff( 
       dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
       ( 
       Dateadd(hh, 11, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, 
       dbo.dt_list_requests.createdon) = 6 
       AND 
       Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, ( 
       Dateadd(mi, 30, 
       ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
       ( 
       Dateadd(hh, 17, ( Dateadd(mi, 30, 
       dbo.dt_list_requests.createdon) )) )) 
       WHEN 
       dbo.dt_list_requests.createdon < Dateadd(hh, 7, ( 
       Dateadd(dd, 0, Datediff 
       (dd, 0, 
       dbo.dt_list_requests.createdon)) )) THEN Dateadd(hh, 11, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) 
       WHEN 
       dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( Dateadd(mi, 30, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 1, ( 
       Dateadd(hh, 11, ( 
       Dateadd(dd, 0, 
       Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN 
       Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, ( 
       Dateadd(mi, 30, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(hh, 17, ( 
       Dateadd(mi, 30, 
       dbo.dt_list_requests.createdon) )) 
       ELSE 
       Dateadd(hh, 4, dbo.dt_list_requests.createdon) 
       END ) THEN NULL 
       WHEN dbo.dt_list_requests.assignment_startdate IS NOT NULL 
       AND dbo.dt_list_requests.assignment_startdate < ( CASE 
                     WHEN 
       Datepart(dw, dbo.dt_list_requests.createdon) = 1 THEN 
                     Dateadd(dd, 1, ( 
                     Dateadd(hh, 11, ( 
                     Dateadd(dd, 0, 
                     Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 7 THEN 
       Dateadd(dd, 2, ( Dateadd(hh, 11, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6 
       AND dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( 
               Dateadd(mi, 30, ( 
               Dateadd(dd, 0, Datediff( 
               dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
                          ( 
       Dateadd(hh, 11, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6 
       AND Dateadd(hh, 4, dbo.dt_list_requests.createdon) > 
       Dateadd(hh, 17, ( 
       Dateadd(mi, 30, 
       ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
                          ( 
       Dateadd(hh, 17, ( Dateadd(mi, 30, 
       dbo.dt_list_requests.createdon) )) )) 
       WHEN dbo.dt_list_requests.createdon < Dateadd(hh, 7, ( 
           Dateadd(dd, 0, Datediff 
           (dd, 0, 
       dbo.dt_list_requests.createdon)) )) THEN Dateadd(hh, 11, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) 
       WHEN dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( 
           Dateadd(mi, 30, ( 
           Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 1, ( 
                      Dateadd(hh, 11, ( 
                      Dateadd(dd, 0, 
                      Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Dateadd(hh, 4, dbo.dt_list_requests.createdon) > 
       Dateadd(hh, 17, ( 
       Dateadd(mi, 30, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(hh, 17, ( 
       Dateadd(mi, 30, dbo.dt_list_requests.createdon) )) 
       ELSE Dateadd(hh, 4, dbo.dt_list_requests.createdon) 
       END ) THEN NULL 
       WHEN Min(dbo.assignment.enddate) 
       OVER ( 
       partition BY dbo.assignment.requestid) < ( CASE 
                  WHEN 
       Datepart(dw, dbo.dt_list_requests.createdon) = 1 THEN 
                  Dateadd(dd, 1, ( 
                  Dateadd(hh, 11, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 7 THEN 
       Dateadd(dd, 2, ( Dateadd(hh, 11, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6 
       AND dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( 
           Dateadd(mi, 30, ( 
           Dateadd(dd, 0, Datediff( 
           dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
                      ( 
       Dateadd(hh, 11, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6 
       AND Dateadd(hh, 4, dbo.dt_list_requests.createdon) > 
       Dateadd(hh, 17, ( 
       Dateadd(mi, 30, 
       ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 3, 
                      ( 
       Dateadd(hh, 17, ( Dateadd(mi, 30, 
       dbo.dt_list_requests.createdon) )) )) 
       WHEN dbo.dt_list_requests.createdon < Dateadd(hh, 7, ( 
       Dateadd(dd, 0, Datediff 
       (dd, 0, 
       dbo.dt_list_requests.createdon)) )) THEN Dateadd(hh, 11, ( 
               Dateadd(dd, 0, 
               Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) 
       WHEN dbo.dt_list_requests.createdon > Dateadd(hh, 17, ( Dateadd(mi, 30, ( 
       Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(dd, 1, ( 
                  Dateadd(hh, 11, ( 
                  Dateadd(dd, 0, 
                  Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) 
       WHEN Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, ( 
       Dateadd(mi, 30, ( Dateadd(dd, 0, Datediff(dd, 0, 
       dbo.dt_list_requests.createdon)) )) )) THEN Dateadd(hh, 17, ( 
       Dateadd(mi, 30, dbo.dt_list_requests.createdon) )) 
       ELSE Dateadd(hh, 4, dbo.dt_list_requests.createdon) 
       END ) THEN NULL 
       ELSE '1' 
       END) AS Number_Breached 
FROM   dbo.request 
       INNER JOIN dbo.user_ 
               ON dbo.request.solvedbyuserid = dbo.user_.userid 
       INNER JOIN dbo.person 
               ON dbo.user_.personorganisationalunitid = dbo.person.ciid 
       INNER JOIN dbo.assignment 
               ON dbo.request.assignmentid = dbo.assignment.assignmentid 
       INNER JOIN dbo.dt_list_requests 
               ON dbo.request.requestid = dbo.dt_list_requests.requestid 
WHERE  ( dbo.person.jobtitle LIKE '%IS 1st Line Support%' ) 
       AND dbo.request.datecreated > Dateadd(month, Datediff(month, 0, Getdate() 
                                                    ) - 1, 
                                     0) 
       AND dbo.request.datecreated < Dateadd(month, Datediff(month, -1, Getdate( 
                                                    )) - 1, 
                                     -1) 
       AND dbo.request.requesttypeid = '1' 

1 个答案:

答案 0 :(得分:0)

就像在子查询中抛出所有代码并执行计数一样简单。 BTW http://poorsql.com/是一种快速,轻松地格式化代码的绝佳工具。

SELECT COUNT(Col_to_Be_Counted) AS Number_Breached
FROM
(
    SELECT CASE 
                WHEN dbo.assignment.enddate IS NOT NULL
                    AND dbo.assignment.enddate < (
                        CASE 
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 1
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 7
                                THEN Dateadd(dd, 2, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))))
                            WHEN dbo.dt_list_requests.createdon < Dateadd(hh, 7, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                                THEN Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                            WHEN dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))
                            ELSE Dateadd(hh, 4, dbo.dt_list_requests.createdon)
                            END
                        )
                    THEN NULL
                WHEN dbo.dt_list_requests.respondedtoon IS NOT NULL
                    AND dbo.dt_list_requests.respondedtoon < (
                        CASE 
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 1
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 7
                                THEN Dateadd(dd, 2, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))))
                            WHEN dbo.dt_list_requests.createdon < Dateadd(hh, 7, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                                THEN Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                            WHEN dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))
                            ELSE Dateadd(hh, 4, dbo.dt_list_requests.createdon)
                            END
                        )
                    THEN NULL
                WHEN dbo.dt_list_requests.assignment_startdate IS NOT NULL
                    AND dbo.dt_list_requests.assignment_startdate < (
                        CASE 
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 1
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 7
                                THEN Dateadd(dd, 2, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))))
                            WHEN dbo.dt_list_requests.createdon < Dateadd(hh, 7, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                                THEN Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                            WHEN dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))
                            ELSE Dateadd(hh, 4, dbo.dt_list_requests.createdon)
                            END
                        )
                    THEN NULL
                WHEN Min(dbo.assignment.enddate) OVER (PARTITION BY dbo.assignment.requestid) < (
                        CASE 
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 1
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 7
                                THEN Dateadd(dd, 2, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Datepart(dw, dbo.dt_list_requests.createdon) = 6
                                AND Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 3, (Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))))
                            WHEN dbo.dt_list_requests.createdon < Dateadd(hh, 7, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                                THEN Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))
                            WHEN dbo.dt_list_requests.createdon > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(dd, 1, (Dateadd(hh, 11, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                            WHEN Dateadd(hh, 4, dbo.dt_list_requests.createdon) > Dateadd(hh, 17, (Dateadd(mi, 30, (Dateadd(dd, 0, Datediff(dd, 0, dbo.dt_list_requests.createdon))))))
                                THEN Dateadd(hh, 17, (Dateadd(mi, 30, dbo.dt_list_requests.createdon)))
                            ELSE Dateadd(hh, 4, dbo.dt_list_requests.createdon)
                            END
                        )
                    THEN NULL
                ELSE '1'
                END AS Col_to_Be_Counted
    FROM dbo.request
    INNER JOIN dbo.user_ ON dbo.request.solvedbyuserid = dbo.user_.userid
    INNER JOIN dbo.person ON dbo.user_.personorganisationalunitid = dbo.person.ciid
    INNER JOIN dbo.assignment ON dbo.request.assignmentid = dbo.assignment.assignmentid
    INNER JOIN dbo.dt_list_requests ON dbo.request.requestid = dbo.dt_list_requests.requestid
    WHERE (dbo.person.jobtitle LIKE '%IS 1st Line Support%')
        AND dbo.request.datecreated > Dateadd(month, Datediff(month, 0, Getdate()) - 1, 0)
        AND dbo.request.datecreated < Dateadd(month, Datediff(month, - 1, Getdate()) - 1, - 1)
        AND dbo.request.requesttypeid = '1'
) A