[英]How To get the Last 15 minutes values(sessions) for each row in the table in sql select with SUM
I want to get a table of
我想要一张桌子
1> requestTime 2> NumberOfSessionsInLast15minutes
1> requestTime 2> NumberOfSessionsInLast15分钟
How to write the query to get a table where the requestTime column will be same and the NumberOfSessions column will be sum of sessions in last 15 minutes
如何编写查询以获取表,其中requestTime列将相同,而NumberOfSessions列将是最近15分钟内的会话总数
EXAMPLE:
例:
The number of sessions for this one 2012-06-06 00:12:00.0000 should be sum of sessions betwen 2012-06-06 00:12:00.0000 and (2012-06-06 00:12:00.0000) - 15 minutes .
此一2012-06-06 00:12:00.0000的会话数应为2012-06-06 00:12:00.0000和(2012-06-06 00:12:00.0000)-15分钟之间的会话数之和。
You could use a subquery to retrieve the number of sessions: 您可以使用子查询来检索会话数:
select s1.requesttime
, (
select sum(NumberOfSessions)
from sessions s2
where dateadd(minute, -15, s1.requesttime) < s2.requesttime
and s2.requesttime <= s1.requesttime
) as TotalNumberOfSessions
from sessions s1
DECLARE
@reportTime DATETIME
SELECT
@reportTime = '2012-06-06 00:12'
SELECT
@reportTime,
SUM(NumberOfSessions)
FROM
yourTable
WHERE
requestTime > DATEADD(minute, -15, @reportTime)
AND requestTime <= @reportTime
Mix and match >=
, >
, <
and <=
depending on your needs and the exact behaviour of your data. 根据需要和数据的确切行为混合并匹配
>=
, >
, <
和<=
。
(Normally a 15 minute window would be >= 00:00 AND < 00:15
, but your definition is slightly different from that.) (通常,一个15分钟的窗口是
>= 00:00 AND < 00:15
,但您的定义与此稍有不同。)
If you want it for all records in the source table... 如果您想要源表中的所有记录...
SELECT
base.requestTime,
SUM(history.NumberOfSessions)
FROM
yourTable AS base
INNER JOIN
yourTable AS history
ON history.requestTime > DATEADD(minute, -15, base.requestTime)
AND history.requestTime <= base.requestTime
GROUP BY
base.requestTime
Here's a way to do it generically, providing a breakdown of 15-minute periods for a given day: 这是一种通用的方法,可提供给定日期15分钟的细分:
DECLARE @requests TABLE(requestTime DATETIME, NumberOfSessions INT);
INSERT @requests SELECT '20120605 23:59', 2
UNION ALL SELECT '20120606 00:00', 500
UNION ALL SELECT '20120606 00:07', 400
UNION ALL SELECT '20120606 00:17', 300
UNION ALL SELECT '20120606 23:57', 500
UNION ALL SELECT '20120607 00:00', 100;
DECLARE @day SMALLDATETIME; -- this would be a stored procedure parameter
SET @day = '20120606';
;WITH n AS
(
SELECT TOP 96 n = DATEADD(MINUTE, 15*
(ROW_NUMBER() OVER (ORDER BY [object_id])-1), @day)
FROM sys.all_columns ORDER BY [object_id]
)
SELECT requestTime = n.n, SumNumberOfSessions = COALESCE(SUM(NumberOfSessions), 0)
FROM n LEFT OUTER JOIN @requests AS r
ON r.requestTime >= n
AND r.requestTime < DATEADD(MINUTE, 15, n)
GROUP BY n.n
ORDER BY requestTime;
Results: 结果:
requestTime SumNumberOfSessions
----------------------- -------------------
2012-06-06 00:00:00.000 900
2012-06-06 00:15:00.000 300
2012-06-06 00:30:00.000 0
...
2012-06-06 23:30:00.000 0
2012-06-06 23:45:00.000 500
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.