【问题标题】:Grouping records based on required time interval in SQL Server在 SQL Server 中根据所需的时间间隔对记录进行分组
【发布时间】:2017-12-17 15:14:22
【问题描述】:

如果我的记录如下所示,有 4 列 Criteria, value, startdate, enddate。从可用的startdate 中,我需要按时间间隔对值进行分组。

如果startdate 是 8.56 而interval 是 10 分钟,那么我需要将记录从 8.56 分组到 9.05)

criteria    Value   startdate                   EndDate

exceptions  5       2017-12-13 08:56:00.000     2017-12-13 09:00:00.000
exceptions  2       2017-12-13 09:01:00.000     2017-12-13 09:05:00.000
exceptions  1       2017-12-13 09:06:00.000     2017-12-13 09:10:00.000
exceptions  3       2017-12-13 09:11:00.000     2017-12-13 09:15:00.000
exceptions  1       2017-12-13 09:16:00.000     2017-12-13 09:20:00.000

我想按要求的时间间隔(例如 10 分钟、12 分钟和 15 分钟)对记录进行分组。

如果间隔是10分钟,那么结果应该如下图,(aggregation-sum(value))

exceptions  7   2017-12-13 08:56:00.000     2017-12-13 09:05:00.000
exceptions  4   2017-12-13 09:06:00.000     2017-12-13 09:15:00.000
exceptions  1   2017-12-13 09:16:00.000     2017-12-13 09:20:00.000 

我如何做到这一点?

【问题讨论】:

  • 提供CREATE TABLE DDL 和INSERT 语句,而不是图片。
  • 由于对齐问题,我将其粘贴为图像。
  • 使用 SQL 脚本,格式化为代码,对齐不会有问题。
  • 你们的团体出游是从当天的第一次开始吗?

标签: sql-server date aggregate-functions


【解决方案1】:

尝试以下方法:

declare @tab table (criteria varchar(100), [value] int, startdate datetime, enddate datetime)

insert into @tab
select 'exceptions', 5, '2017-12-13 8:56:00.000', '2017-12-13 9:0:0.000'
union all
select 'exceptions', 2, '2017-12-13 9:01:00.000', '2017-12-13 9:05:0.000'
union all
select 'exceptions', 1, '2017-12-13 9:06:00.000', '2017-12-13 9:10:0.000'
union all
select 'exceptions', 3, '2017-12-13 9:11:00.000', '2017-12-13 9:15:0.000'
union all
select 'exceptions', 1, '2017-12-13 9:16:00.000', '2017-12-13 9:20:0.000'

declare @interval int = 10--15,20   --change here
declare @start int = 10--15,20      --also change here

declare @i int = 1
declare @stdt datetime
declare @eddt datetime
declare @tab_new table (criteria varchar(100), [value] int, interval int, grp int, start_dt datetime, end_date datetime)
while ((select count(1) from @tab) > 0)
begin
    set @stdt = (select top 1 startdate from @tab)
    set @eddt = (select top 1 enddate from @tab)

    insert into @tab_new
    select criteria, [value], @interval - DATEDIFF(MINUTE, dateadd(minute, -1, startdate), enddate), @i, @stdt, @eddt from @tab where startdate = @stdt and enddate = @eddt
    set @interval -= DATEDIFF(MINUTE, dateadd(minute, -1, @stdt), @eddt)

    delete from @tab where startdate = @stdt and enddate = @eddt
    if @interval = 0 begin set @i += 1 set @interval = @start end
end

select criteria, sum([value]) [value], min(start_dt) startdate, max(end_date) enddate from @tab_new
group by criteria, grp

HTH。

【讨论】:

    【解决方案2】:

    以下是基于 this answer 的特定要求的基于集合的示例:

    DECLARE
          @MinuteInterval int = 10
        , @StartDate datetime2(3)
        , @EndDate datetime2(3);
    SELECT
          @StartDate = MIN(StartDate)
        , @EndDate = MAX(EndDate)
    FROM dbo.Example;
    WITH intervals AS (
        SELECT
              criteria
            , value
            , DATEADD(minute, (DATEDIFF(minute, @StartDate, StartDate) / @MinuteInterval) * @MinuteInterval, @StartDate) AS StartInterval
            , EndDate
        FROM dbo.Example
        )
    SELECT
          criteria
        , SUM(value) AS ValueCount
        , StartInterval
        , CASE WHEN DATEADD(minute, @MinuteInterval - 1, StartInterval) < @EndDate 
            THEN DATEADD(minute, @MinuteInterval-1, StartInterval)
            ELSE @EndDate END AS EndInterval
    FROM intervals
    GROUP BY
          criteria
        , StartInterval
    ORDER BY
          criteria
        , StartInterval;
    

    【讨论】:

      猜你喜欢
      • 2015-01-03
      • 1970-01-01
      • 2018-08-24
      • 2015-03-07
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多