如何产生连续时间?(案例)

Posted insus

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何产生连续时间?(案例)相关的知识,希望对你有一定的参考价值。

原问题如下:

技术图片

 

 

Insus.NET的问题解决,分2步,1,创建一个辅助表,2,合并数据。

程式码搜寻出来如图:

技术图片

 

技术图片
IF OBJECT_ID(NTEMPDB.DBO.#search_data) IS NOT NULL
DROP TABLE #search_data
GO
 
CREATE TABLE #search_data ([times] DATETIME,[val] INT)
 
INSERT INTO #search_data ([times],[val]) VALUES
(2001-01-01 00:00:00,13),
(2001-01-01 00:10:00,22),
(2001-01-01 00:20:00,31),
(2001-01-01 00:40:00,5 ),
(2001-01-01 00:50:00,64),
(2001-01-01 02:30:00,73),
(2001-01-01 04:10:00,63),
(2001-01-01 04:50:00,12),
(2001-01-01 06:30:00,32)
 
Source Code

 

技术图片

 

接下来,我们可以创建一张辅助表:

 技术图片

 

技术图片
DECLARE @start_datetime DATETIME = 2000-01-01 00:00:00,
        @end_datetime DATETIME  = 2000-02-01 00:00:00

;WITH DTs([times]) 
AS
(
    SELECT  @start_datetime
    UNION ALL
    SELECT [times] = DATEADD(MINUTE,10, [times])
    FROM [DTs]
    WHERE [times] < @end_datetime
)
SELECT [times] FROM [DTs] OPTION (MaxRecursion 0)
Source Code

 

总共产生4465行记录。

更多相关辅助表,可以参考《使用CTE生成辅助表(数字或时间)等https://www.cnblogs.com/insus/p/10946112.html 和《快速生成基数的辅助表https://www.cnblogs.com/insus/p/10946112.html

 

为了更好操作,把辅助表的产生数据存入一张临时表中:

技术图片

 

技术图片
IF OBJECT_ID(NTEMPDB.DBO.#base) IS NOT NULL
    DROP TABLE #base
GO
 
CREATE TABLE #base ([times] DATETIME)

DECLARE @start_datetime DATETIME = 2000-01-01 00:00:00,
        @end_datetime DATETIME  = 2000-02-01 00:00:00
;WITH DTs([times]) 
AS
(
    SELECT  @start_datetime
    UNION ALL
    SELECT [times] = DATEADD(MINUTE,10, [times])
    FROM [DTs]
    WHERE [times] < @end_datetime
)

INSERT INTO #base ([times]) SELECT [times] FROM [DTs] OPTION (MaxRecursion 0)
Source Code

 

第2步,合并数据,把程序搜索结果与辅助表的数据进行合并merge:

技术图片

 

技术图片
MERGE #search_data AS Target
    USING (SELECT [times] FROM #base) AS Source
    ON (Target.[times] = Source.[times])
 
    WHEN NOT MATCHED BY TARGET THEN
        INSERT ([times],[val]) VALUES ([times],0);  

SELECT [times],[val] FROM #search_data
Source Code

 

OK,实现方法仅供参考,完整代码:

技术图片
IF OBJECT_ID(NTEMPDB.DBO.#search_data) IS NOT NULL
DROP TABLE #search_data
GO
 
CREATE TABLE #search_data ([times] DATETIME,[val] INT)
 
INSERT INTO #search_data ([times],[val]) VALUES
(2001-01-01 00:00:00,13),
(2001-01-01 00:10:00,22),
(2001-01-01 00:20:00,31),
(2001-01-01 00:40:00,5 ),
(2001-01-01 00:50:00,64),
(2001-01-01 02:30:00,73),
(2001-01-01 04:10:00,63),
(2001-01-01 04:50:00,12),
(2001-01-01 06:30:00,32)
 
 
 
IF OBJECT_ID(NTEMPDB.DBO.#base) IS NOT NULL
    DROP TABLE #base
GO
 
CREATE TABLE #base ([times] DATETIME)

DECLARE @start_datetime DATETIME = 2000-01-01 00:00:00,
        @end_datetime DATETIME  = 2000-02-01 00:00:00
;WITH DTs([times]) 
AS
(
    SELECT  @start_datetime
    UNION ALL
    SELECT [times] = DATEADD(MINUTE,10, [times])
    FROM [DTs]
    WHERE [times] < @end_datetime
)

INSERT INTO #base ([times]) SELECT [times] FROM [DTs] OPTION (MaxRecursion 0)



MERGE #search_data AS Target
    USING (SELECT [times] FROM #base) AS Source
    ON (Target.[times] = Source.[times])
 
    WHEN NOT MATCHED BY TARGET THEN
        INSERT ([times],[val]) VALUES ([times],0);  

SELECT [times],[val] FROM #search_data
Source Code

 

以上是关于如何产生连续时间?(案例)的主要内容,如果未能解决你的问题,请参考以下文章

代码在计算最大连续 1 时失败 4/9 案例,我该如何改进?

如何优化C ++代码的以下片段 - 卷中的零交叉

为啥连续抛出 2 个异常不会产生无法访问的代码警告?

问题11:如何进行反向迭代 & 如何实现反向迭代

将数据从Activity发送到Fragment - 如何?

Java mp4parser 片段序列不连续