经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
如何产生连续时间?(案例)
来源:cnblogs  作者:Insus.NET  时间:2019/6/17 8:57:23  对本文有异议

原问题如下:

 

 

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

程式码搜寻出来如图:

 

  1. IF OBJECT_ID(N'TEMPDB.DBO.#search_data') IS NOT NULL
  2. DROP TABLE #search_data
  3. GO
  4. CREATE TABLE #search_data ([times] DATETIME,[val] INT)
  5. INSERT INTO #search_data ([times],[val]) VALUES
  6. ('2001-01-01 00:00:00',13),
  7. ('2001-01-01 00:10:00',22),
  8. ('2001-01-01 00:20:00',31),
  9. ('2001-01-01 00:40:00',5 ),
  10. ('2001-01-01 00:50:00',64),
  11. ('2001-01-01 02:30:00',73),
  12. ('2001-01-01 04:10:00',63),
  13. ('2001-01-01 04:50:00',12),
  14. ('2001-01-01 06:30:00',32)
Source Code

 

 

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

 

 

  1. DECLARE @start_datetime DATETIME = '2000-01-01 00:00:00',
  2. @end_datetime DATETIME = '2000-02-01 00:00:00'
  3. ;WITH DTs([times])
  4. AS
  5. (
  6. SELECT @start_datetime
  7. UNION ALL
  8. SELECT [times] = DATEADD(MINUTE,10, [times])
  9. FROM [DTs]
  10. WHERE [times] < @end_datetime
  11. )
  12. 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

 

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

 

  1. IF OBJECT_ID(N'TEMPDB.DBO.#base') IS NOT NULL
  2. DROP TABLE #base
  3. GO
  4. CREATE TABLE #base ([times] DATETIME)
  5. DECLARE @start_datetime DATETIME = '2000-01-01 00:00:00',
  6. @end_datetime DATETIME = '2000-02-01 00:00:00'
  7. ;WITH DTs([times])
  8. AS
  9. (
  10. SELECT @start_datetime
  11. UNION ALL
  12. SELECT [times] = DATEADD(MINUTE,10, [times])
  13. FROM [DTs]
  14. WHERE [times] < @end_datetime
  15. )
  16. INSERT INTO #base ([times]) SELECT [times] FROM [DTs] OPTION (MaxRecursion 0)
Source Code

 

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

 

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

 

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

  1. IF OBJECT_ID(N'TEMPDB.DBO.#search_data') IS NOT NULL
  2. DROP TABLE #search_data
  3. GO
  4. CREATE TABLE #search_data ([times] DATETIME,[val] INT)
  5. INSERT INTO #search_data ([times],[val]) VALUES
  6. ('2001-01-01 00:00:00',13),
  7. ('2001-01-01 00:10:00',22),
  8. ('2001-01-01 00:20:00',31),
  9. ('2001-01-01 00:40:00',5 ),
  10. ('2001-01-01 00:50:00',64),
  11. ('2001-01-01 02:30:00',73),
  12. ('2001-01-01 04:10:00',63),
  13. ('2001-01-01 04:50:00',12),
  14. ('2001-01-01 06:30:00',32)
  15. IF OBJECT_ID(N'TEMPDB.DBO.#base') IS NOT NULL
  16. DROP TABLE #base
  17. GO
  18. CREATE TABLE #base ([times] DATETIME)
  19. DECLARE @start_datetime DATETIME = '2000-01-01 00:00:00',
  20. @end_datetime DATETIME = '2000-02-01 00:00:00'
  21. ;WITH DTs([times])
  22. AS
  23. (
  24. SELECT @start_datetime
  25. UNION ALL
  26. SELECT [times] = DATEADD(MINUTE,10, [times])
  27. FROM [DTs]
  28. WHERE [times] < @end_datetime
  29. )
  30. INSERT INTO #base ([times]) SELECT [times] FROM [DTs] OPTION (MaxRecursion 0)
  31. MERGE #search_data AS Target
  32. USING (SELECT [times] FROM #base) AS Source
  33. ON (Target.[times] = Source.[times])
  34. WHEN NOT MATCHED BY TARGET THEN
  35. INSERT ([times],[val]) VALUES ([times],0);
  36. SELECT [times],[val] FROM #search_data
Source Code

 

原文链接:http://www.cnblogs.com/insus/p/11025759.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号