绘制时间序列时无法在Grafana中使用SQL案例语句?
试图在Grafana v8.3.3中使用SQL查询来绘制一个时间序列,以提醒是否在上一个小时内没有收到订单,但仅在本质上是在营业时间(即上午8点至晚上10点)。
由于警报只能每小时运行,因此我将在非企业时间(即上午11点至上午7点)入侵SQL查询以返回1个,因此警报不会发射。
尝试使用案例,但似乎并不认为该值返回为一个数字 - 关于如何最好地解决此问题或在Grafana中实现上述任何想法?
提前致谢!
SELECT $__timeGroup(DATE_CREATED, '1h', 0),
1 AS "BOOKED"
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)
SELECT $__timeGroup(DATE_CREATED, '1h', 0),
CASE WHEN TO_CHAR(DATE_CREATED,'HH24') >= 8 AND to_char(DATE_CREATED,'HH24') <= 22) THEN COUNT(1) ELSE 1 END) AS "BOOKED"
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)
Trying to use a SQL query in Grafana v8.3.3 to plot a time-series to alert if no orders recieved in a previous hour, but only during essentially business hours (i.e 8am - 10pm).
Since the alert can only be run hourly, I'm going to hack the SQL query to return 1 during non-business hours (i.e 11pm - 7am) so alert will not fire.
Tried using CASE but doesn't seem to see the value returned as being a number - any thoughts on how best to fix this, or implement the above in Grafana?
Thanks in advance!
SELECT $__timeGroup(DATE_CREATED, '1h', 0),
1 AS "BOOKED"
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)
SELECT $__timeGroup(DATE_CREATED, '1h', 0),
CASE WHEN TO_CHAR(DATE_CREATED,'HH24') >= 8 AND to_char(DATE_CREATED,'HH24') <= 22) THEN COUNT(1) ELSE 1 END) AS "BOOKED"
FROM CHANNEL_ORDER
WHERE STATUS = 'BOOKED'
GROUP BY $__timeGroup(DATE_CREATED, '1h', 0)
ORDER BY $__timeGroup(DATE_CREATED, '1h', 0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的情况 表达 有两个问题。首先,您将
date_created
作为参数传递,但不在的组中,仅
$ __ timeGroup(date_created,'1h',0)
是。中的参考
或汇总功能内部。第二,您将日期转换为字符串,然后将其与整数进行比较;这种比较是不可能的,因此SQL Engine 隐式 基于数据类型顺序将两者之一转换为使它们具有相同的datatypeq。在您的情况下,整数转换为字符串。
由于
$ __ TimeGroup()
宏返回以整数秒为单位的时间,我会建议您使用Modulo的
%
,例如 ...我 推断 您的基础数据库是PostgreSQL吗?另外,请记住,您对
,0
在宏调用中的使用意味着没有任何数据的任何小时将根据0
的结果分配。 ,不是1
。案例表达的替代方案可能是...Your case expression has two problems. First, you pass
DATE_CREATED
in as a parameter, but it's not in theGROUP BY
, only$__timeGroup(DATE_CREATED, '1h', 0)
is.SELECT
must either be in theGROUP BY
or inside an aggregate function.Second you convert the date to a string, and compare it to an integer; this comparison is not possible and so the SQL engine implicitly converts one of the two, based on datatype order of precedence, to make them the same datatypeq. In your case, the integer is converted to a string.
As the
$__timegroup()
macro return the time rounded as an integer number of seconds, I would instead suggest something like...Note: I used
%
for modulo, as I'm inferring the your underlying database is PostgreSQL?Also, bear in mind that your use of
, 0
in the macro call means that any hours without any data will be assigned a result of0
, not1
. An alternative to a CASE expression could be...