如何打破 n 个相等间隔之间的时间范围?

How break a time range between n numbers of equal intervals?

TimeMin: 2015-04-29 10:57:56.623

时间最大值: 2015-04-29 11:04:35.133

我正在尝试编写一个 select 查询以将其分成 n 等间隔

这是我的尝试:

declare @Min int
select @Min  = min(DATEDIFF(ss,'1970-01-01', biddate)) from tbl_bids
declare @Max int
select @Max  = max(DATEDIFF(ss,'1970-01-01', biddate)) from tbl_bids

declare @NumParts int 
select @NumParts =COUNT(*) from tbl_bids

select ((DATEDIFF(ss,'1970-01-01', biddate) * (@Max - @Min) / @NumParts) + 1) - ((@Max - @Min) / @NumParts), 
(DATEDIFF(ss,'1970-01-01', biddate) * (@Max - @Min) / @NumParts) + 1
from tbl_bids 
where DATEDIFF(ss,'1970-01-01', biddate)<= @NumParts

但是 returns 0 rows.

示例:

最小值: 2015-04-29 10:50:00

最大值: 2015-04-29 11:00:00

if NumParts = 5(分成5等分)

输出应该是:

2015-04-29 10:52:00
2015-04-29 10:54:00
2015-04-29 10:56:00
2015-04-29 10:58:00
2015-04-29 11:00:00

您可以获得日期之间的总秒数差异,然后使用它来获得相等的部分。像这样。

DECLARE @dt_min DATETIME = '2015-04-29 10:50:00'

DECLARE @dt_max DATETIME = '2015-04-29 11:00:00'

DECLARE @parts INT = 5
DECLARE @sec BIGINT = DATEDIFF(SECOND,@dt_min,@dt_max)/@parts

SELECT TOP (@parts) DATEADD(second,@sec*(ROW_NUMBER()OVER(ORDER BY (SELECT 1)) - 1) ,@dt_min) FROM sys.tables

你的查询应该是这样的。

declare @dt_min DATETIME
select @dt_min  = min(biddate) from tbl_bids
declare @dt_max DATETIME
select @dt_max  = max(biddate) from tbl_bids

declare @NumParts int 
select @NumParts =COUNT(*) from tbl_bids

DECLARE @sec BIGINT = DATEDIFF(SECOND,@dt_min,@dt_max)/@NumParts

select DATEADD(second,@sec*(ROW_NUMBER()OVER(ORDER BY biddate) - 1) ,@dt_min)
from tbl_bids