0
喜的上下文中使用有一點是用於我們的電話記錄系統SQL的,我想看到許多調用如何突破4小時SLQ性反應的時間SQL窗功能無法在其他窗口函數或聚合
,我有問題的是,第33行我使用MIN,並得到一個錯誤,因爲我也使用COUNT
(消息4109,級別15,狀態1,行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'
將其中一個替換爲子查詢。 – 2015-03-25 13:00:47