試試這樣說:
此功能將創建運行數字的派生表(最高可達10^9)無中生有和快如閃電:
CREATE FUNCTION dbo.RunningNumbers
(
@start INT
,@count INT
,@step INT
)
RETURNS TABLE
AS
RETURN
WITH x AS(SELECT 1 AS N FROM(VALUES(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) AS tbl(N))--10^1
,N3 AS (SELECT 1 AS N FROM x CROSS JOIN x AS N2 CROSS JOIN x N3) --10^3
,Tally AS(SELECT TOP(@count) (ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) + @start -1) * @step AS Nr FROM N3
CROSS JOIN N3 N6 CROSS JOIN N3 AS N9)
SELECT Nr
FROM Tally;
GO
- 這是與APPLY
一起使用該功能的方法。該CTE將先切秒:
DECLARE @tbl_user TABLE(UserID INT IDENTITY,startTime DATETIME,endTime DateTime,note VARCHAR(100));
INSERT INTO @tbl_user VALUES
({ts'2016-11-28 08:00:00'},{ts'2016-11-28 08:00:30'},'less than one')
,({ts'2016-11-28 08:01:00'},{ts'2016-11-28 08:05:12'},'some minutes')
,({ts'2016-11-28 23:50:00'},{ts'2016-11-29 00:10:23'},'over midnight');
WITH CutToNakedMinute AS
(
SELECT UserID
,note
,CONVERT(DATETIME,CONVERT(VARCHAR(17),startTime,126)+'00',126) AS StartMinute
,CONVERT(DATETIME,CONVERT(VARCHAR(17),endTime,126)+'00',126) AS EndMinute
FROM @tbl_user AS u
)
SELECT UserID
,note
,StartMinute
,DATEADD(MINUTE,steps.Nr,StartMinute) AS RunningMinute
FROM CutToNakedMinute
OUTER APPLY dbo.RunningNumbers(0,DATEDIFF(MINUTE,StartMinute,EndMinute)+1,1) AS steps
GO
--Clean-up for testing
DROP FUNCTION dbo.RunningNumbers;
結果
+--------+---------------+-------------------------+-------------------------+
| UserID | note | StartMinute | RunningMinute |
+--------+---------------+-------------------------+-------------------------+
| 1 | less than one | 2016-11-28 08:00:00.000 | 2016-11-28 08:00:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 2 | some minutes | 2016-11-28 08:01:00.000 | 2016-11-28 08:01:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 2 | some minutes | 2016-11-28 08:01:00.000 | 2016-11-28 08:02:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 2 | some minutes | 2016-11-28 08:01:00.000 | 2016-11-28 08:03:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 2 | some minutes | 2016-11-28 08:01:00.000 | 2016-11-28 08:04:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 2 | some minutes | 2016-11-28 08:01:00.000 | 2016-11-28 08:05:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:50:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:51:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:52:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:53:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:54:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:55:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:56:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:57:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:58:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-28 23:59:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:00:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:01:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:02:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:03:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:04:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:05:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:06:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:07:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:08:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:09:00.000 |
+--------+---------------+-------------------------+-------------------------+
| 3 | over midnight | 2016-11-28 23:50:00.000 | 2016-11-29 00:10:00.000 |
+--------+---------------+-------------------------+-------------------------+
先試一下,然後問一個問題。 SO不會爲你編碼。 – Sachith
你爲什麼要這樣做? –
首先我嘗試使用一個視圖(見下文)。但服務器無法完成並中止。似乎太複雜了。 SELECT TOP(100)PERCENT dbo.view_users_woid.userID,dbo.view_users_woid.startdate,dbo.view_users_woid.enddate, dbo.view_minutes.datetime FROM dbo.view_minutes INNER JOIN dbo.view_users_woid ON dbo.view_minutes.datetime> = dbo.view_users_woid.startdate AND dbo.view_minutes.datetime <= dbo.view_users_woid.enddate ORDER BY dbo.view_users_woid.userID,dbo.view_minutes.datetime – avidan