Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I go about getting the percentage of items filtered in a SQLite query

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading