I am trying to make a grafana timeseries chart that shows the percentage of queries blocked from pihole. The query I made is below but the results are definitely not right and I think it is because of how I am handling the where clause.
SELECT
timestamp as time,
100.0 * (SELECT COUNT( * ) as query_count FROM queries WHERE status IN (1, 4, 5, 6, 7, 8, 9, 10, 11, 15)) / COUNT( * ) as percentage
FROM
queries
GROUP BY
time;
>Solution :
If I understand correctly then use conditional aggregation. The following query will, for each group, give you the percentage of queries having the specific status:
SELECT
timestamp AS time,
100.0 * COUNT(CASE WHEN status IN (1, 4, 5, ...) THEN 1 END) / COUNT(*) AS percentage
FROM
queries
GROUP BY
time
Or:
SELECT
timestamp AS time,
AVG(CASE WHEN status IN (1, 4, 5, ...) THEN 100.0 ELSE 0.0 END) AS percentage
FROM
queries
GROUP BY
time