Last active
April 26, 2022 19:08
-
-
Save ipatalas/4b3f052ef361ed0bd9222ca1304ae6ed to your computer and use it in GitHub Desktop.
Home assistant helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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