Grafana SQL查询,将字符串转换为数字

时间:2019-02-19 17:34:24

标签: mysql sql grafana

Grafana查询

Select UNIX_TIMESTAMP(created_at) as time_sec,
    metric_value as value,
    'Metric Value' as metric
from lumenis_db.telemetry
where $__timeFilter(created_at)

收到以下错误:

Value column must have numeric datatype, column: value type: string

如何通过查询将metric_value从字符串转换为数字?

编辑

我的SQL:

CREATE TABLE lumenis_db.telemetry (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at DATETIME(3) NOT NULL,
device_type VARCHAR(35) NOT NULL,
device_serial VARCHAR(35) NOT NULL,
metric_name VARCHAR(35) NULL,
metric_value VARCHAR(35) NULL
);

2 个答案:

答案 0 :(得分:1)

如果metric_value包含作为存储为String的数字的值,则可以使用MySQL的CONVERT函数,例如:

SELECT UNIX_TIMESTAMP(created_at) as time_sec,
    CONVERT(metric_value, UNSIGNED) as value,
    'Metric Value' as metric
FROM lumenis_db.telemetry
WHERE $__timeFilter(created_at)

HereCONVERT函数的文档。

答案 1 :(得分:-1)

SELECT UNIX_TIMESTAMP(created_at) as time_sec,
    CAST(metric_value as SIGNED integer) as value,
    'Metric Value' as metric
FROM lumenis_db.telemetry
WHERE $__timeFilter(created_at)

成功了