如何在两次之间每小时增加一个计数

时间:2019-01-10 16:17:10

标签: sql sql-server sql-server-2008-r2

我不确定这是否可行,但我想显示结果在条目有效的每个小时的两个日期/时间之间的条目有效:

RcvDateTime        FinishDateTime     Qty
----------------   ----------------   ---
2019-01-09 22:00   NULL               1
2019-01-10 09:00   2019-01-10 10:10   3
2019-01-10 11:10   2019-01-10 13:10   2
2019-01-10 09:10   2019-01-10 13:25   1
2019-01-10 12:00   2019-01-10 14:15   2
2019-01-10 13:20   NULL               1

查询结果我正在寻找是否要在2019-01-10 15:00进行查询

Hour    Count
----    -----
01      1       (Row 1)
02      1       (Row 1)
03      1       (Row 1)
04      1       (Row 1)
05      1       (Row 1)
06      1       (Row 1)
07      1       (Row 1)
08      1       (Row 1)        
09      5       (Rows 1,2,4)
10      5       (Rows 1,2,4)
11      4       (Rows 1,3,4)
12      6       (Rows 1,3,4,5)
13      7       (Rows 1,3,4,5,6)
14      3       (Rows 1,5,6)

我可以获取当前有效的任何行的数据:

SELECT DATEPART(hour,GETDATE()) AS Hour, SUM(Qty) AS Count WHERE FinishDateTime IS NULL

,但是努力将过去的小时数添加到小时中(在RcvDateTime和FinishDateTime之间。)

我们将不胜感激地收到任何想法,提示或什至是可能的。

2 个答案:

答案 0 :(得分:1)

如果我了解您的整个查询,我认为CTE和group by的正确联接查询可能会降低您的例外结果。

看看这个相关示例:

CREATE TABLE #MyTable  
(PrimaryKey   int PRIMARY KEY,  
   DateValueBegin      DATETIME,
   DateValueEnd      DATETIME,
   NbValue      int
  );  
GO  

INSERT INTO #MyTable 
SELECT 1, DATEADD(HOUR,-2,GETDATE()), GETDATE(), 2
UNION
SELECT 2, DATEADD(HOUR,-2,GETDATE()), DATEADD(HOUR,-1,GETDATE()), 4
UNION 
SELECT 3, DATEADD(HOUR,-2,GETDATE()), GETDATE(), 3
UNION
SELECT 4, DATEADD(HOUR,-1,GETDATE()), NULL, 2
UNION
SELECT 5, DATEADD(HOUR,-1,GETDATE()), NULL, 2;

with mycte as
(
    SELECT 0 AS MyHour
    UNION ALL
    SELECT MyHour + 1
    FROM mycte 
    WHERE MyHour + 1 < 24
)
SELECT convert(date, DateValueBegin) AS 'DAY' , MyHour AS 'Hour', SUM(NbValue)
FROM #MyTable right join mycte on  (DATEPART(HOUR,DateValueBegin) <=  MyHour AND (DateValueEnd IS NULL OR DATEPART(HOUR,DateValueEnd) >=  MyHour))
Group By convert(date, DateValueBegin), MyHour;




DROP TABLE  #MyTable

我有更新代码以使用间隔日期

结果:

DAY         Hour    Total
NULL        12  NULL
NULL        13  NULL
NULL        14  NULL
2019-01-10  15  9
2019-01-10  16  13
2019-01-10  17  9
2019-01-10  18  4
2019-01-10  19  4
2019-01-10  20  4
2019-01-10  21  4
2019-01-10  22  4
2019-01-10  23  4

编辑以保留现有日期

CREATE TABLE #MyTable  
(PrimaryKey   int PRIMARY KEY,  
   DateValueBegin      DATETIME,
   DateValueEnd      DATETIME,
   NbValue      int
  );  
GO  

INSERT INTO #MyTable 
SELECT 1, DATEADD(HOUR,-2,GETDATE()), GETDATE(), 2
UNION
SELECT 2, DATEADD(HOUR,-2,GETDATE()), DATEADD(HOUR,-1,GETDATE()), 4
UNION 
SELECT 3, DATEADD(HOUR,-2,GETDATE()), GETDATE(), 3
UNION
SELECT 4, DATEADD(HOUR,-1,GETDATE()), NULL, 2
UNION
SELECT 5, DATEADD(HOUR,-1,GETDATE()), NULL, 2
UNION
SELECT 6, DATEADD(DAY,-1,GETDATE()), DATEADD(HOUR,1,DATEADD(DAY,-1,GETDATE())), 2
UNION
SELECT 7, DATEADD(DAY,-1,GETDATE()), DATEADD(HOUR,3,DATEADD(DAY,-1,GETDATE())), 2;

with mycte as
(
    SELECT 0 AS MyHour
    UNION ALL
    SELECT MyHour + 1
    FROM mycte 
    WHERE MyHour + 1 < 24
)
SELECT convert(date, ISNULL(DateValueBegin,MyDay)) AS 'DAY' , MyHour AS 'Hour', SUM(ISNULL(NbValue,0)) AS TOTAL
FROM #MyTable 
right join(select distinct convert(date, DateValueBegin) MyDay, MyHour from mycte, #MyTable) AS SubDayHour on  (DATEPART(HOUR,DateValueBegin) <=  MyHour AND ((DateValueEnd IS NULL  AND  MyHour < DATEPART(HOUR,GETDATE())) OR DATEPART(HOUR,DateValueEnd) >=  MyHour)) AND convert(date, DateValueBegin) = MyDay
Group By convert(date, ISNULL(DateValueBegin,MyDay)), MyHour
ORDER BY convert(date, ISNULL(DateValueBegin,MyDay)) ASC, MyHour ASC;


SELECT * FROM #MyTable ;


DROP TABLE  #MyTable

新结果:

DAY Hour    TOTAL
2019-01-10  0   0
2019-01-10  1   0
2019-01-10  2   0
2019-01-10  3   0
2019-01-10  4   0
2019-01-10  5   0
2019-01-10  6   0
2019-01-10  7   0
2019-01-10  8   0
2019-01-10  9   0
2019-01-10  10  0
2019-01-10  11  4
2019-01-10  12  4
2019-01-10  13  2
2019-01-10  14  2
2019-01-10  15  0
2019-01-10  16  0
2019-01-10  17  0
2019-01-10  18  0
2019-01-10  19  0
2019-01-10  20  0
2019-01-10  21  0
2019-01-10  22  0
2019-01-10  23  0
2019-01-11  0   0
2019-01-11  1   0
2019-01-11  2   0
2019-01-11  3   0
2019-01-11  4   0
2019-01-11  5   0
2019-01-11  6   0
2019-01-11  7   0
2019-01-11  8   0
2019-01-11  9   9
2019-01-11  10  13
2019-01-11  11  5
2019-01-11  12  0
2019-01-11  13  0
2019-01-11  14  0
2019-01-11  15  0
2019-01-11  16  0
2019-01-11  17  0
2019-01-11  18  0
2019-01-11  19  0
2019-01-11  20  0
2019-01-11  21  0
2019-01-11  22  0
2019-01-11  23  0

答案 1 :(得分:0)

尝试此操作,如果您需要其他分组,只需更改日期即可分组。另外,如果只希望日期范围包含一个where子句。

SELECT CAST(FinishDateTime as date) AS ForDate,
       DATEPART(hour,FinishDateTime) AS OnHour,
       SUM(QTY) AS SumPerTimeGroup
FROM YourTable
GROUP BY CAST(FinishDateTime as date),
       DATEPART(hour,FinishDateTime)