示例數據可能有助於解釋我試圖做的比解釋它更多,所以我將從此開始。計算指定窗口的滾動計數
這裏是我目前處理的數據:
+-------------------------+--------------+
| CallStart | CallDuration |
+-------------------------+--------------+
| 2017-09-15 09:15:15.313 | 00:01:28 |
| 2017-09-15 09:15:15.317 | 00:01:45 |
| 2017-09-15 09:16:45.603 | 00:01:31 |
| 2017-09-15 09:17:00.637 | 00:01:24 |
| 2017-09-15 09:18:20.853 | 00:01:42 |
| 2017-09-15 09:18:25.870 | 00:01:24 |
| 2017-09-15 11:27:05.117 | 00:00:59 |
| 2017-09-15 11:31:16.053 | 00:01:18 |
| 2017-09-15 11:34:41.627 | 00:01:00 |
| 2017-09-15 12:16:45.413 | 00:01:01 |
| 2017-09-15 12:18:15.820 | 00:01:05 |
| 2017-09-15 12:30:43.607 | 00:01:04 |
| 2017-09-15 12:31:48.817 | 00:00:55 |
| 2017-09-15 12:35:14.563 | 00:00:59 |
| 2017-09-15 12:42:10.947 | 00:00:43 |
| 2017-09-15 12:56:28.807 | 00:01:14 |
| 2017-09-15 13:05:10.643 | 00:00:37 |
| 2017-09-15 13:20:08.400 | 00:00:37 |
| 2017-09-15 14:30:12.607 | 00:00:59 |
| 2017-09-15 14:31:22.807 | 00:00:49 |
| 2017-09-15 15:19:47.240 | 00:01:07 |
| 2017-09-15 16:04:47.753 | 00:00:55 |
| 2017-09-15 16:58:08.080 | 00:00:55 |
| 2017-09-15 17:05:04.557 | 00:00:50 |
| 2017-09-15 17:20:42.753 | 00:00:58 |
| 2017-09-15 17:28:09.140 | 00:01:05 |
| 2017-09-15 17:39:46.690 | 00:00:38 |
| 2017-09-15 17:40:21.957 | 00:01:05 |
| 2017-09-15 17:43:47.570 | 00:01:08 |
| 2017-09-15 17:47:23.390 | 00:01:05 |
| 2017-09-15 17:47:28.410 | 00:00:56 |
| 2017-09-15 17:51:59.380 | 00:01:04 |
+-------------------------+--------------+
我試圖讓滾動COUNT(*)
的出現在這個數據在15分鐘的時間內完成數。該數據預期的結果將是以下幾點:
+-------------------------+--------------+------------------+
| CallStart | CallDuration | DropsIn15Minutes |
+-------------------------+--------------+------------------+
| 2017-09-15 09:15:15.313 | 00:01:28 | 1 |
| 2017-09-15 09:15:15.317 | 00:01:45 | 2 |
| 2017-09-15 09:16:45.603 | 00:01:31 | 3 |
| 2017-09-15 09:17:00.637 | 00:01:24 | 4 |
| 2017-09-15 09:18:20.853 | 00:01:42 | 5 |
| 2017-09-15 09:18:25.870 | 00:01:24 | 6 |
| 2017-09-15 11:27:05.117 | 00:00:59 | 1 |
| 2017-09-15 11:31:16.053 | 00:01:18 | 2 |
| 2017-09-15 11:34:41.627 | 00:01:00 | 3 |
| 2017-09-15 12:16:45.413 | 00:01:01 | 1 |
| 2017-09-15 12:18:15.820 | 00:01:05 | 2 |
| 2017-09-15 12:30:43.607 | 00:01:04 | 3 |
| 2017-09-15 12:31:48.817 | 00:00:55 | 3 |
| 2017-09-15 12:35:14.563 | 00:00:59 | 3 |
| 2017-09-15 12:42:10.947 | 00:00:43 | 4 |
| 2017-09-15 12:56:28.807 | 00:01:14 | 2 |
| 2017-09-15 13:05:10.643 | 00:00:37 | 2 |
| 2017-09-15 13:20:08.400 | 00:00:37 | 2 |
| 2017-09-15 14:30:12.607 | 00:00:59 | 1 |
| 2017-09-15 14:31:22.807 | 00:00:49 | 2 |
| 2017-09-15 15:19:47.240 | 00:01:07 | 1 |
| 2017-09-15 16:04:47.753 | 00:00:55 | 1 |
| 2017-09-15 16:58:08.080 | 00:00:55 | 1 |
| 2017-09-15 17:05:04.557 | 00:00:50 | 2 |
| 2017-09-15 17:20:42.753 | 00:00:58 | 1 |
| 2017-09-15 17:28:09.140 | 00:01:05 | 2 |
| 2017-09-15 17:39:46.690 | 00:00:38 | 2 |
| 2017-09-15 17:40:21.957 | 00:01:05 | 3 |
| 2017-09-15 17:43:47.570 | 00:01:08 | 3 |
| 2017-09-15 17:47:23.390 | 00:01:05 | 4 |
| 2017-09-15 17:47:28.410 | 00:00:56 | 5 |
| 2017-09-15 17:51:59.380 | 00:01:04 | 6 |
+-------------------------+--------------+------------------+
的樣本數據:
Create Table #Calls
(
CallStart DateTime,
CallDuration Time(0)
);
Insert Into #Calls
Values (N'2017-09-15T09:15:15.313', N'00:01:28'),
(N'2017-09-15T09:15:15.317', N'00:01:45'),
(N'2017-09-15T09:16:45.603', N'00:01:31'),
(N'2017-09-15T09:17:00.637', N'00:01:24'),
(N'2017-09-15T09:18:20.853', N'00:01:42'),
(N'2017-09-15T09:18:25.87', N'00:01:24'),
(N'2017-09-15T11:27:05.117', N'00:00:59'),
(N'2017-09-15T11:31:16.053', N'00:01:18'),
(N'2017-09-15T11:34:41.627', N'00:01:00'),
(N'2017-09-15T12:16:45.413', N'00:01:01'),
(N'2017-09-15T12:18:15.82', N'00:01:05'),
(N'2017-09-15T12:30:43.607', N'00:01:04'),
(N'2017-09-15T12:31:48.817', N'00:00:55'),
(N'2017-09-15T12:35:14.563', N'00:00:59'),
(N'2017-09-15T12:42:10.947', N'00:00:43'),
(N'2017-09-15T12:56:28.807', N'00:01:14'),
(N'2017-09-15T13:05:10.643', N'00:00:37'),
(N'2017-09-15T13:20:08.4', N'00:00:37'),
(N'2017-09-15T14:30:12.607', N'00:00:59'),
(N'2017-09-15T14:31:22.807', N'00:00:49'),
(N'2017-09-15T15:19:47.24', N'00:01:07'),
(N'2017-09-15T16:04:47.753', N'00:00:55'),
(N'2017-09-15T16:58:08.08', N'00:00:55'),
(N'2017-09-15T17:05:04.557', N'00:00:50'),
(N'2017-09-15T17:20:42.753', N'00:00:58'),
(N'2017-09-15T17:28:09.14', N'00:01:05'),
(N'2017-09-15T17:39:46.69', N'00:00:38'),
(N'2017-09-15T17:40:21.957', N'00:01:05'),
(N'2017-09-15T17:43:47.57', N'00:01:08'),
(N'2017-09-15T17:47:23.39', N'00:01:05'),
(N'2017-09-15T17:47:28.41', N'00:00:56'),
(N'2017-09-15T17:51:59.38', N'00:01:04');
我可以有些得到這個通過了以下工作:
Select CallStart,
CallDuration,
DropsIn15Minutes =
(
Select Count(*)
From #Calls C2
Where C2.CallStart Between DateAdd(Minute, -15, C1.CallStart)
And C1.CallStart
)
From #Calls C1
但是,我想避免子查詢,以COUNT(*) OVER()
(或任何其他解決方案,如果可能的話)爲例。
這可能嗎?或者子查詢是否適合這個問題?其他
爲什麼一些連續的行具有相同的數字?你是否需要每組或連續數字的總數? –
@VamsiPrabhala一些連續的行具有相同的數字,因爲他們之間有足夠的時間過去了,一個人已經從15分鐘的窗口中走出。這只是查看記錄的計數,沒有分組。 – Siyual
毫秒精度真的很重要,還是我們可以將毫秒截斷爲秒? –