From 3f5eaf86a869783b4dae7078f49cd7ebe60b4909 Mon Sep 17 00:00:00 2001 From: Nikita Bulavin Date: Mon, 11 Nov 2024 22:10:28 +0300 Subject: [PATCH] Sort the categories by their usage --- .../cashsense/core/database/dao/CategoryDao.kt | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/core/database/src/main/kotlin/ru/resodostudios/cashsense/core/database/dao/CategoryDao.kt b/core/database/src/main/kotlin/ru/resodostudios/cashsense/core/database/dao/CategoryDao.kt index 0031067ff..5700e214a 100644 --- a/core/database/src/main/kotlin/ru/resodostudios/cashsense/core/database/dao/CategoryDao.kt +++ b/core/database/src/main/kotlin/ru/resodostudios/cashsense/core/database/dao/CategoryDao.kt @@ -12,7 +12,17 @@ interface CategoryDao { @Query("SELECT * FROM categories WHERE id = :id") fun getCategoryEntity(id: String): Flow - @Query("SELECT * FROM categories ORDER BY id DESC") + @Query( + value = """ + SELECT c.id, c.title, c.icon_id FROM categories c + LEFT JOIN ( + SELECT tc.category_id, COUNT(tc.category_id) AS usage_count + FROM transactions_categories tc + GROUP BY tc.category_id + ) AS usage ON c.id = usage.category_id + ORDER BY usage.usage_count DESC; + """, + ) fun getCategoryEntities(): Flow> @Upsert