选择具有按ID分组的日期期间的列的总和

时间:2017-05-31 09:06:49

标签: sql date select group-by sum

我需要按照第二天的11:00到10:00的时间段选择值的总和,按ID分组。

这是我的表1:

          date          value    id
"2017-03-29 11:00:00"   "453"   "id1"   
"2017-03-29 12:00:00"   "000"   "id1"   
"2017-03-29 13:00:00"   "234"   "id1"   
"2017-03-29 14:00:00"   "000"   "id1"   
"2017-03-29 15:00:00"   "000"   "id1"   
"2017-03-29 16:00:00"   "222"   "id1"   
"2017-03-29 17:00:00"   "000"   "id1"   
"2017-03-29 18:00:00"   "000"   "id1"   
"2017-03-29 19:00:00"   "000"   "id1"   
"2017-03-29 20:00:00"   "000"   "id1"   
"2017-03-29 21:00:00"   "000"   "id1"   
"2017-03-29 22:00:00"   "000"   "id1"   
"2017-03-29 23:00:00"   "123"   "id1"   
"2017-03-30 00:00:00"   "000"   "id1"   
"2017-03-30 01:00:00"   "000"   "id1"   
"2017-03-30 02:00:00"   "000"   "id1"   
"2017-03-30 03:00:00"   "000"   "id1"   
"2017-03-30 04:00:00"   "000"   "id1"   
"2017-03-30 05:00:00"   "000"   "id1"   
"2017-03-30 07:00:00"   "000"   "id1"   
"2017-03-30 08:00:00"   "000"   "id1"   
"2017-03-30 09:00:00"   "000"   "id1"   
"2017-03-30 10:00:00"   "333"   "id1"   
"2017-03-30 11:00:00"   "000"   "id1"   
"2017-03-30 12:00:00"   "123"   "id1"   
"2017-03-30 13:00:00"   "000"   "id1"   
"2017-03-30 14:00:00"   "000"   "id1"   
"2017-03-30 15:00:00"   "000"   "id1"   
"2017-03-30 16:00:00"   "000"   "id1"   
"2017-03-30 18:00:00"   "000"   "id1"   
"2017-03-30 19:00:00"   "000"   "id1"   
"2017-03-30 20:00:00"   "000"   "id1"   
"2017-03-30 21:00:00"   "000"   "id1"   
"2017-03-30 22:00:00"   "000"   "id1"   
"2017-03-30 23:00:00"   "000"   "id1"   
"2017-03-31 00:00:00"   "000"   "id1"   
"2017-03-31 01:00:00"   "000"   "id1"   
"2017-03-31 02:00:00"   "000"   "id1"   
"2017-03-31 03:00:00"   "000"   "id1"   
"2017-03-31 04:00:00"   "000"   "id1"   
"2017-03-31 05:00:00"   "000"   "id1"   
"2017-03-31 06:00:00"   "000"   "id1"   
"2017-03-31 07:00:00"   "000"   "id1"   
"2017-03-31 08:00:00"   "000"   "id1"   
"2017-03-31 09:00:00"   "000"   "id1"   
"2017-03-29 12:00:00"   "756"   "id2"   
"2017-03-29 13:00:00"   "701"   "id2"   
"2017-03-29 14:00:00"   "758"   "id2"   
"2017-03-29 15:00:00"   "867"   "id2"   
"2017-03-29 16:00:00"   "924"   "id2"   
"2017-03-29 17:00:00"   "871"   "id2"   
"2017-03-29 18:00:00"   "817"   "id2"   
"2017-03-29 19:00:00"   "109"   "id2"   
"2017-03-29 20:00:00"   "131"   "id2"   
"2017-03-29 21:00:00"   "126"   "id2"   
"2017-03-29 22:00:00"   "120"   "id2"   
"2017-03-29 23:00:00"   "120"   "id2"   
"2017-03-30 00:00:00"   "930"   "id2"   
"2017-03-30 01:00:00"   "108"   "id2"   
"2017-03-30 09:00:00"   "867"   "id2"   
"2017-03-30 10:00:00"   "808"   "id2"   
"2017-03-30 11:00:00"   "756"   "id2"   
"2017-03-30 12:00:00"   "758"   "id2"   
"2017-03-30 13:00:00"   "866"   "id2"   
"2017-03-30 14:00:00"   "981"   "id2"   
"2017-03-30 15:00:00"   "981"   "id2"   
"2017-03-30 16:00:00"   "872"   "id2"   
"2017-03-30 17:00:00"   "928"   "id2"   
"2017-03-30 18:00:00"   "109"   "id2"   
"2017-03-30 19:00:00"   "115"   "id2"   
"2017-03-30 20:00:00"   "121"   "id2"   
"2017-03-30 21:00:00"   "131"   "id2"   
"2017-03-30 22:00:00"   "148"   "id2"   
"2017-03-30 23:00:00"   "132"   "id2"   
"2017-03-31 00:00:00"   "115"   "id2"   
"2017-03-31 01:00:00"   "120"   "id2"   
"2017-03-31 02:00:00"   "137"   "id2"   
"2017-03-31 03:00:00"   "137"   "id2"   
"2017-03-31 04:00:00"   "132"   "id2"   
"2017-03-31 05:00:00"   "126"   "id2"   
"2017-03-31 06:00:00"   "126"   "id2"   
"2017-03-31 07:00:00"   "104"   "id2"   
"2017-03-31 08:00:00"   "874"   "id2"   
"2017-03-31 09:00:00"   "651"   "id2"   
"2017-03-31 10:00:00"   "540"   "id2"   

结果表应如下所示:

date_period                                  value_sum   id
"2017-03-29 11:00:00 - 2017-03-30 10:00:00"    1365     "id1"
"2017-03-30 11:00:00 - 2017-03-31 10:00:00"    123      "id1"
"2017-03-29 11:00:00 - 2017-03-30 10:00:00"    9013     "id2"
"2017-03-30 11:00:00 - 2017-03-31 10:00:00"    9834     "id2"

表1这只是一个例子,真实的桌子有很多ID和大日期从2015年到2017年,不仅从2017-03-29到2017-03-31。我需要按第二天的11:00到10:00按时间段选择所有值的总和,按ID分组。

Oracle和MSSQL中应该有2个解决方案。

5 个答案:

答案 0 :(得分:1)

我认为以下查询将帮助您解决问题,如果您有任何其他问题,请告诉我

DECLARE @ProductTotals TABLE
    (
        Daytime Datetime, 
      ProductID int, 
      id nvarchar(50)
    )
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 11:00:00',453,'id1')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-04-30 12:00:00',568,'id1')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-05-31 13:00:00',500,'id1')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 11:00:00',586,'id2')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 12:00:00',895,'id2')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 13:00:00',452,'id2')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 11:00:00',875,'id3')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 12:00:00',586,'id3')
    INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 13:00:00',784,'id3')


SELECT sum(t.sumid) AS sumid,
       T.id,
       T.Duration from(
SELECT p.ProductID AS sumid,
       p.id,
       Duration = cast(DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '10:00:00') AS varchar)+' - ' + cast(DATEADD(DAY, DATEDIFF(DAY, -1, Daytime), '11:00:00') AS varchar)
FROM @ProductTotals p
GROUP BY p.id,
         p.Daytime,
         p.ProductID
HAVING Daytime > DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '10:00:00')
AND Daytime < DATEADD(DAY, DATEDIFF(DAY, -1, Daytime), '11:00:00')

    )T 
group by T.id,t.Duration

OutPut is Given below 

568     id1 Apr 30 2017 10:00AM - May  1 2017 11:00AM
453     id1 Mar 29 2017 10:00AM - Mar 30 2017 11:00AM
1933    id2 Mar 29 2017 10:00AM - Mar 30 2017 11:00AM
2245    id3 Mar 29 2017 10:00AM - Mar 30 2017 11:00AM
500     id1 May 31 2017 10:00AM - Jun  1 2017 11:00AM

答案 1 :(得分:1)

检查此答案是否有效

DECLARE @ProductTotals TABLE
    (
        Daytime Datetime, 
      ProductID int, 
      id nvarchar(50)
    )
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 11:00:00',453,'id1')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-04-30 12:00:00',568,'id1')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-05-31 13:00:00',500,'id1')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 11:00:00',586,'id2')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 12:00:00',895,'id2')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 13:00:00',452,'id2')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 11:00:00',875,'id3')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 12:00:00',586,'id3')
INSERT INTO @ProductTotals (Daytime,ProductID, id) values('2017-03-29 13:00:00',784,'id3')

   ;with cte(id, duration, productsum)
as (

select id, Duration = cast(DATEADD(day, DATEDIFF(day, 0, Daytime), '10:00:00') as varchar) +' - ' +  cast(DATEADD(day, DATEDIFF(day, -1, Daytime), '11:00:00') as varchar),  
ProductSum = sum(ProductId) from @ProductTotals

 group by id, 

Daytime having Daytime > DATEADD(day, DATEDIFF(day, 0, Daytime), '10:00:00')
and Daytime < DATEADD(day, DATEDIFF(day, -1, Daytime), '11:00:00')

)

select id, Duration, ProductSum = Sum(ProductSum) from cte group by id,Duration order by id




  id    Duration                            ProductSum
id1 Apr 30 2017 10:00AM - May  1 2017 11:00AM   568
id1 Mar 29 2017 10:00AM - Mar 30 2017 11:00AM   453
id1 May 31 2017 10:00AM - Jun  1 2017 11:00AM   500
id2 Mar 29 2017 10:00AM - Mar 30 2017 11:00AM   1933
id3 Mar 29 2017 10:00AM - Mar 30 2017 11:00AM   2245

答案 2 :(得分:0)

逻辑上你想要做以下事情: 取日期值,减去11小时,(将所有时间转移到相同的&#39;天&#39;) 将结果日期时间转换为天。 (即删除时间组件) 这为您提供了一个&#34; ReportingDate&#34;

然后根据ReportingDate&amp; amp; ID

答案 3 :(得分:0)

MSSQL的解决方案。

初始数据:

DECLARE @Data TABLE
(
     [date] DATETIME, 
     [value] INT, 
     [id] NVARCHAR(MAX)
);

INSERT INTO @Data 
VALUES
({ ts'2017-03-29 11:00:00' }, 453, 'id1'),
({ ts'2017-03-29 12:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 13:00:00' }, 234, 'id1'),   
({ ts'2017-03-29 14:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 15:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 16:00:00' }, 222, 'id1'),   
({ ts'2017-03-29 17:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 18:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 19:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 20:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 21:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 22:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 23:00:00' }, 123, 'id1'),   
({ ts'2017-03-30 00:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 01:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 02:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 03:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 04:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 05:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 07:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 08:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 09:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 10:00:00' }, 333, 'id1'),   
({ ts'2017-03-30 11:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 12:00:00' }, 123, 'id1'),   
({ ts'2017-03-30 13:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 14:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 15:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 16:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 18:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 19:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 20:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 21:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 22:00:00' }, 000, 'id1'),   
({ ts'2017-03-30 23:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 00:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 01:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 02:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 03:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 04:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 05:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 06:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 07:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 08:00:00' }, 000, 'id1'),   
({ ts'2017-03-31 09:00:00' }, 000, 'id1'),   
({ ts'2017-03-29 12:00:00' }, 756, 'id2'),   
({ ts'2017-03-29 13:00:00' }, 701, 'id2'),   
({ ts'2017-03-29 14:00:00' }, 758, 'id2'),   
({ ts'2017-03-29 15:00:00' }, 867, 'id2'),   
({ ts'2017-03-29 16:00:00' }, 924, 'id2'),   
({ ts'2017-03-29 17:00:00' }, 871, 'id2'),   
({ ts'2017-03-29 18:00:00' }, 817, 'id2'),   
({ ts'2017-03-29 19:00:00' }, 109, 'id2'),   
({ ts'2017-03-29 20:00:00' }, 131, 'id2'),   
({ ts'2017-03-29 21:00:00' }, 126, 'id2'),   
({ ts'2017-03-29 22:00:00' }, 120, 'id2'),   
({ ts'2017-03-29 23:00:00' }, 120, 'id2'),   
({ ts'2017-03-30 00:00:00' }, 930, 'id2'),   
({ ts'2017-03-30 01:00:00' }, 108, 'id2'),   
({ ts'2017-03-30 09:00:00' }, 867, 'id2'),   
({ ts'2017-03-30 10:00:00' }, 808, 'id2'),   
({ ts'2017-03-30 11:00:00' }, 756, 'id2'),   
({ ts'2017-03-30 12:00:00' }, 758, 'id2'),   
({ ts'2017-03-30 13:00:00' }, 866, 'id2'),   
({ ts'2017-03-30 14:00:00' }, 981, 'id2'),   
({ ts'2017-03-30 15:00:00' }, 981, 'id2'),   
({ ts'2017-03-30 16:00:00' }, 872, 'id2'),   
({ ts'2017-03-30 17:00:00' }, 928, 'id2'),   
({ ts'2017-03-30 18:00:00' }, 109, 'id2'),   
({ ts'2017-03-30 19:00:00' }, 115, 'id2'),   
({ ts'2017-03-30 20:00:00' }, 121, 'id2'),   
({ ts'2017-03-30 21:00:00' }, 131, 'id2'),   
({ ts'2017-03-30 22:00:00' }, 148, 'id2'),   
({ ts'2017-03-30 23:00:00' }, 132, 'id2'),   
({ ts'2017-03-31 00:00:00' }, 115, 'id2'),   
({ ts'2017-03-31 01:00:00' }, 120, 'id2'),   
({ ts'2017-03-31 02:00:00' }, 137, 'id2'),   
({ ts'2017-03-31 03:00:00' }, 137, 'id2'),   
({ ts'2017-03-31 04:00:00' }, 132, 'id2'),   
({ ts'2017-03-31 05:00:00' }, 126, 'id2'),   
({ ts'2017-03-31 06:00:00' }, 126, 'id2'),   
({ ts'2017-03-31 07:00:00' }, 104, 'id2'),   
({ ts'2017-03-31 08:00:00' }, 874, 'id2'),   
({ ts'2017-03-31 09:00:00' }, 651, 'id2'),   
({ ts'2017-03-31 10:00:00' }, 540, 'id2');

解决方案查询:

SELECT
     CONVERT(VARCHAR(255), CAST(DATEADD(HOUR, 11, CAST(CAST(DATEADD(HOUR, -11, [date]) 
        AS DATE) AS DATETIME)) AS DATETIME), 120) + ' - ' + 
     CONVERT(VARCHAR(255), CAST(DATEADD(HOUR, 11 + 23, CAST(CAST(DATEADD(HOUR, -11, [date]) 
        AS DATE) AS DATETIME)) AS DATETIME), 120) AS date_period,
     SUM([value]) AS value_sum, [id]
FROM @Data AS [Data]
WHERE CAST(DATEADD(HOUR, -11, [date]) AS TIME) <= CAST({ t'23:00:00'} AS TIME)
GROUP BY CAST(DATEADD(HOUR, -11, [date]) AS DATE), [id]

结果:

date_period                                     value_sum   id
----------------------------------------------- ----------- --------
2017-03-29 11:00:00 - 2017-03-30 10:00:00       1365        id1
2017-03-30 11:00:00 - 2017-03-31 10:00:00       123         id1
2017-03-29 11:00:00 - 2017-03-30 10:00:00       9013        id2
2017-03-30 11:00:00 - 2017-03-31 10:00:00       9960        id2

答案 4 :(得分:0)

此解决方案正常:

   SELECT t4.id, (t3.sumid + t4.sumid) as summ, t3.Duration_id  FROM(
    /* 1st day sum start from from 11:00:00 to 00:00:00*/
    (SELECT sum(t.sumid) AS sumid, 
           T.id,
           T.Duration, T.Duration_id as Duration_id from(
    SELECT p.Value AS sumid,
           p.id,
           Duration = 'from ' + cast(DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '11:00:00') AS varchar),
           Duration_id = 'from ' + cast(DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '11:00:00') AS varchar) + ' to ' + cast(DATEADD(DAY, DATEDIFF(DAY, -1, Daytime), '10:00:00') AS varchar)+id
    FROM ProductTotals p
    GROUP BY p.id,
             p.Daytime,
             p.Value
    HAVING Daytime >= DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '11:00:00')
        )T 
    group by T.id,t.Duration, t.Duration_id)t3
    /* 1st day sum stop from 11:00:00 to 00:00:00*/
     INNER JOIN 
    /* 2nd day sum start from 00:00:00 to 10:00:00*/
    (SELECT sum(t2.sumid) AS sumid, 
           T2.id,
           T2.Duration, t2.Duration_id as Duration_id from(
    SELECT p.Value AS sumid,
           p.id,
           Duration = 'from ' + cast(DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '10:00:00') AS varchar),
           Duration_id = 'from ' + cast(DATEADD(DAY, DATEDIFF(DAY, 1, Daytime), '11:00:00') AS varchar) + ' to ' + cast(DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '10:00:00') AS varchar)+id
    FROM ProductTotals p
    GROUP BY p.id,
             p.Daytime,
             p.Value
    HAVING Daytime <= DATEADD(DAY, DATEDIFF(DAY, 0, Daytime), '10:00:00')
    /* 2nd day sum stop from 00:00:00 to 10:00:00*/
        )T2 
    group by T2.id, t2.Duration, t2.Duration_id)t4

    ON T3.Duration_id = T4.Duration_id
    )

    order by t4.id, t3.Duration_id