Skip to content

Instantly share code, notes, and snippets.

@ipatalas
Last active April 26, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipatalas/4b3f052ef361ed0bd9222ca1304ae6ed to your computer and use it in GitHub Desktop.
Save ipatalas/4b3f052ef361ed0bd9222ca1304ae6ed to your computer and use it in GitHub Desktop.
Home assistant helpers
SELECT COUNT(1) INTO @total FROM states;
WITH t AS (
SELECT ROW_NUMBER() OVER (ORDER BY `count` DESC) AS row_num, entity_id, count(1) `count`, ROUND(100 * COUNT(1) / @total, 2) percent_of_total
FROM `states`
GROUP BY entity_id
ORDER BY 2 DESC
)
SELECT *, SUM(percent_of_total) OVER (ORDER BY `row_num`) AS cumulative
FROM t
SELECT SUM(LENGTH(IFNULL(attributes, 0))) INTO @total_size FROM states;
WITH t AS (
SELECT ROW_NUMBER() OVER (ORDER BY `size` DESC) AS row_num, entity_id, count(1) `count`, SUM(LENGTH(IFNULL(attributes, 0))) size, ROUND(100 * SUM(LENGTH(IFNULL(attributes, 0))) / @total_size, 2) AS percent_of_total
FROM `states`
GROUP BY entity_id
ORDER BY size DESC
)
SELECT *, SUM(percent_of_total) OVER (ORDER BY `row_num`) AS cumulative
FROM t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment