[英]Android SQLite sort entries by frequency without grouping
I am trying to sort the database records by row value frequency in descending order but without grouping the entries. 我试图按行值频率按降序排序数据库记录,但不对条目进行分组。 Excuse me, I am a total beginner in databases.
对不起,我是数据库的初学者。
Here is the full code: 这是完整的代码:
SELECT *
FROM table_name
WHERE date_time >= 10
and date_time <= 100
GROUP BY activity_type
ORDER BY COUNT(activity_type) DESC
And here is the relevant code: 以下是相关代码:
SELECT *
FROM table_name
GROUP BY activity_type
ORDER BY COUNT(activity_type) DESC
This is the desired result (sorted by the frequency of activity_type in a descending order without grouping): 这是期望的结果(按活动类型的频率按降序排序,不分组):
name (irrelevant) activity_type (number)
------------------------------------------------
Rome 1
London 1
Madrid 1
Stockholm 3
Paris 3
Moscow 2
Unfortunately, SQLite doesn't support group by
in subqueries. 不幸的是,SQLite不支持子查询中的
group by
。 So your choices are either an outer join (which creates a cross product of the table by itself and which is then culled with a group by
) or a temporary table which would contain activity_type
counts. 因此,您的选择要么是外部联接(它自己创建表的交叉产品,然后用
group by
拣选),要么是包含activity_type
计数的临时表。
CREATE TABLE activity_count_table(activity_type, activity_count);
INSERT INTO activity_count_table
SELECT activity_type, COUNT(activity_type) AS activity_count
FROM table_name
WHERE date_time BETWEEN 10 AND 100
GROUP BY activity_type;
SELECT *
FROM table_name NATURAL JOIN activity_count_table
WHERE date_time BETWEEN 10 AND 100
ORDER BY activity_count_table.activity_count DESC;
DROP TABLE activity_count_table;
Obviously, this is not an ideal solution. 显然,这不是一个理想的解决方案。 You probably want to have some unique identifier added to the name
activity_count_table
for each call (so that multiple queries don't interfere with each other). 您可能希望为每个调用添加一个名称
activity_count_table
唯一标识符(以便多个查询不会相互干扰)。
It's still better than creating an outer join (which needs n^2 rows in memory) and then culling it with group by
(which really just gives you a diagonal when applied to a self-join). 它仍然比创建外连接(在内存中需要n ^ 2行)然后用
group by
剔除它更好(当应用于自连接时,它实际上只是给你一个对角线)。
If the aggregation must not affect the actual query, you have to move it into a subquery: 如果聚合不得影响实际查询,则必须将其移动到子查询中:
SELECT *
FROM table_name
WHERE date_time BETWEEN 10 AND 100
ORDER BY (SELECT count(*)
FROM table_name AS T2
WHERE T2.activity_type = table_name.activity_type
AND date_time BETWEEN 10 AND 100) DESC;
If your database supports it, you can use a windowing function with the OVER
clause, eg 如果您的数据库支持它,您可以使用带有
OVER
子句的窗口函数,例如
SELECT *
FROM table_name
ORDER BY COUNT(*) OVER (PARTITION BY activity_type) DESC
Confirmed to work on: 确认工作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.