我有一个JDBC数据库(特别是DB2,但我正在寻找一些与DB无关的东西,至少是DB2和Oracle),它有一个表,每隔10分钟就会插入一个记录,其中包含有关应用程序运行的API的统计信息。题。它看起来像:
StatKey, StartDate, EndDate, APIName, StatName, StatValue
201505071498224437562706 2015-05-07 14:12:44.0 2015-05-07 14:22:44.0 API5 Invocations 34
201505071498161437466684 2015-05-07 14:06:14.0 2015-05-07 14:16:14.0 API4 Invocations 79
201505071498060937466556 2015-05-07 13:56:08.0 2015-05-07 14:06:08.0 API4 Average 26,264.37
201505071497263437627286 2015-05-07 14:16:33.0 2015-05-07 14:26:34.0 API2 Invocations 24
201505071497262137620812 2015-05-07 14:16:19.0 2015-05-07 14:26:20.0 API2 Invocations 24
201505071497024537466378 2015-05-07 13:52:43.0 2015-05-07 14:02:44.0 API1 Average 6,830,050
201505071497023337466368 2015-05-07 13:52:31.0 2015-05-07 14:02:32.0 API3 Average 31,523
201505071496023337466361 2015-05-07 13:52:31.0 2015-05-07 14:02:32.0 API2 Invocations 1
201505071494263837628892 2015-05-07 14:16:36.0 2015-05-07 14:26:37.0 API5 Invocations 68
201505071493124437466656 2015-05-07 14:02:44.0 2015-05-07 14:12:44.0 API1 Invocations 2
201505071492263037625304 2015-05-07 14:16:29.0 2015-05-07 14:26:30.0 API3 Average 179,223.29
每隔10分钟,在此期间执行的任何API都会有类似上面的条目。但是,多个JVM将写入同一个数据库,因此开始和结束时间不是每10分钟一次,每小时可能有超过6个条目。
我要做的是创建一个SQL,它会每小时对每小时运行时所有API的所有调用进行分组。例如:
Date&Hour, API, Invocations
2015-05-07 12:00, API1, 100
2015-05-07 12:00, API2, 150
2015-05-07 13:00, API2, 200
etc...
我已尝试根据主键的SUBSTR(始终是时间戳加上一些随机数 - 但小时和分钟之间是2个随机数字)在小时标记处进行GROUP BY但是我&# 39;我不知道如何每小时添加所有StatName = Invocations。
有人可以提供一些关于如何实现这一目标的想法吗?
答案 0 :(得分:1)
不确定你之后是什么?
本质上它会查看YYYYMMDDHH 10个位置,因为它们包含值以进行分组...然后仅基于调用求和
SELECT substr(statKey,1,10) as DH, APIName, Sum(Statvalue) Invocations
FROM TableName
WHERE StatName = 'Invocations'
GROUP BY substr(statKey,1,10), APIName, StatName
示例:
WITH CTE AS
(SELECT '201505071498224437562706' AS StatKey,
'2015-05-07 14:12:44.0' AS StartDate,
'2015-05-07 14:22:44.0' AS EndDate,
'API5' AS APIName,
'Invocations' AS StatName,
34 AS statvalue
FROM dual
UNION ALL
SELECT '201505071498161437466684',
'2015-05-07 14:06:14.0',
'2015-05-07 14:16:14.0',
'API4',
'Invocations',
79
FROM dual
UNION ALL
SELECT '201505071498060937466556',
'2015-05-07 13:56:08.0',
'2015-05-07 14:06:08.0',
'API4',
'Average',
26264.37
FROM dual
)
SELECT substr(statKey,1,10) as DH, APIName, StatName, Sum(Statvalue)
FROM TableName
WHERE StatName = 'Invocations'
GROUP BY substr(statKey,1,10), APIName, StatName
答案 1 :(得分:1)
对于DB2至少,为什么不呢
select date(startdate) as start_date
, hour(startdate) as start_hour
, API
, sum(statvalue) as Invocations
from mytbl
where statname = 'Invocations'
group by date(startdate), hour(startdate), API
如果这就是你真正想要的东西,我会把它作为练习让你把日期和时间重新组合成一个时间戳......
答案 2 :(得分:1)
另一种可能的解决方案:
select to_char(StarDate,'rrrr-mm-dd HH24:')||'00' as DateHour,
APIName as API,
sum(StatValue) as Invocations
from STATISTICS
where StatName = 'Invocations'
group by to_char(StarDate,'rrrr-mm-dd HH24:')||'00', APIName
有不同的方法可以做到这一点。
祝你好运!答案 3 :(得分:1)
Oracle 11g R2架构设置:
CREATE TABLE Data AS
SELECT '201505071498224437562706' AS StatKey, TO_DATE( '2015-05-07 14:12:44', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:22:44', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API5' AS APIName, 'Invocations' AS StatName, 34 AS StatValue FROM DUAL
UNION ALL SELECT '201505071498161437466684' AS StatKey, TO_DATE( '2015-05-07 14:06:14', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:16:14', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API4' AS APIName, 'Invocations' AS StatName, 79 AS StatValue FROM DUAL
UNION ALL SELECT '201505071498060937466556' AS StatKey, TO_DATE( '2015-05-07 13:56:08', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:06:08', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API4' AS APIName, 'Average' AS StatName, 26264.37 AS StatValue FROM DUAL
UNION ALL SELECT '201505071497263437627286' AS StatKey, TO_DATE( '2015-05-07 14:16:33', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:26:34', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API2' AS APIName, 'Invocations' AS StatName, 24 AS StatValue FROM DUAL
UNION ALL SELECT '201505071497262137620812' AS StatKey, TO_DATE( '2015-05-07 14:16:19', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:26:20', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API2' AS APIName, 'Invocations' AS StatName, 24 AS StatValue FROM DUAL
UNION ALL SELECT '201505071497024537466378' AS StatKey, TO_DATE( '2015-05-07 13:52:43', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:02:44', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API1' AS APIName, 'Average' AS StatName, 6830050 AS StatValue FROM DUAL
UNION ALL SELECT '201505071497023337466368' AS StatKey, TO_DATE( '2015-05-07 13:52:31', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:02:32', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API3' AS APIName, 'Average' AS StatName, 31523 AS StatValue FROM DUAL
UNION ALL SELECT '201505071496023337466361' AS StatKey, TO_DATE( '2015-05-07 13:52:31', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:02:32', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API2' AS APIName, 'Invocations' AS StatName, 1 AS StatValue FROM DUAL
UNION ALL SELECT '201505071494263837628892' AS StatKey, TO_DATE( '2015-05-07 14:16:36', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:26:37', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API5' AS APIName, 'Invocations' AS StatName, 68 AS StatValue FROM DUAL
UNION ALL SELECT '201505071493124437466656' AS StatKey, TO_DATE( '2015-05-07 14:02:44', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:12:44', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API1' AS APIName, 'Invocations' AS StatName, 2 AS StatValue FROM DUAL
UNION ALL SELECT '201505071492263037625304' AS StatKey, TO_DATE( '2015-05-07 14:16:29', 'YYYY-MM-DD HH24:MI:SS' ) AS StartDate, TO_DATE( '2015-05-07 14:26:30', 'YYYY-MM-DD HH24:MI:SS' ) AS EndDate, 'API3' AS APIName, 'Average' AS StatName, 179223.29 AS StatValue FROM DUAL;
查询1 :
SELECT TRUNC( EndDate, 'HH' ) AS "Date&Hour",
APIName,
SUM( StatValue ) AS Invocations
FROM Data
WHERE StatName = 'Invocations'
GROUP BY TRUNC( EndDate, 'HH' ),
APIName
<强> Results 强>:
| Date&Hour | APINAME | INVOCATIONS |
|-----------------------|---------|-------------|
| May, 07 2015 14:00:00 | API2 | 49 |
| May, 07 2015 14:00:00 | API5 | 102 |
| May, 07 2015 14:00:00 | API1 | 2 |
| May, 07 2015 14:00:00 | API4 | 79 |
答案 4 :(得分:1)
日期函数似乎很难以数据库无关的方式实现。
对于DB不可知的解决方案,我建议在数据库中创建隐藏特定于DB的代码的实现的视图,因此允许您使用简单的选择而不会出现任何语法问题。