2017-12-17 2 views
-1

내 레코드가 4 열 Criteria, value, startdate, enddate 인 아래에있는 경우 사용 가능하면 startdate 시간 간격으로 값을 그룹화해야합니다. startdate이 8.56이고 interval은 10 분입니다SQL Server에서 필요한 시간 간격을 기준으로 레코드 그룹화

경우에, 나는 8.56에서

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 

I 그룹 십분 12 분 같은 필요한 시간 간격에 기록을하고 싶습니다) 9.05에 기록서에 필요 15 분.

10 분의 간격이, 그 결과는 다음과 같이 할 필요가있는 경우 (aggregation- 합 (값))

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 

어떻게이를 달성 하는가?

+0

,'제공 TABLE' DDL과'INSERT' 문을 만듭니다. –

+0

[시간별 또는 10 분 단위로 그룹화하는 방법] 가능한 복제본 (https://stackoverflow.com/questions/5002661/how-to-group-time-by-hour-or-by-10- 분) –

+0

정렬 문제로 이미지로 붙여 넣었습니다. – Sridevi

답변

0

는 다음과 같은 시도 :

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를.

0

여기 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; 
관련 문제