Questions tagged «group-by»

GROUP BY:通用值的SQL聚合

3
如何取消嵌套和GROUP BY元素的JSON数组?
给定该band表,其中包含json一个数组的列: id | people ---+------------- 1 | ['John', 'Thomas'] 2 | ['John', 'James'] 3 | ['James', 'George'] 如何列出每个名字所属的乐队数量? 所需的输出: name | count -------+------------ John | 2 James | 2 Thomas | 1 George | 1

3
根据最新日期选择具有多个联接的行
我有此查询(SQLFiddle): SELECT c.name, a.user_id, a.status_id, a.title, a.rtime, u.user_name, s.status_name FROM company c LEFT JOIN action a ON a.company_id=c.id LEFT JOIN user u ON u.id=a.user_id LEFT JOIN status s ON s.id=a.status_id WHERE u.user_name='Morgan' -- WHERE c.name='Fiddle' GROUP BY c.id HAVING a.rtime IS NULL OR a.rtime = ( SELECT max(rtime) FROM action …
8 mysql  group-by 

2
使用MAX分组,而不是MAX
我是一名程序员,正在处理一个具有以下方案的大表: UpdateTime, PK, datetime, notnull Name, PK, char(14), notnull TheData, float 上有一个聚集索引 Name, UpdateTime 我想知道什么应该更快: SELECT MAX(UpdateTime) FROM [MyTable] 要么 SELECT MAX([UpdateTime]) AS value from ( SELECT [UpdateTime] FROM [MyTable] group by [UpdateTime] ) as t 此表的插入是50,000行中具有相同日期的数据块。因此,我认为分组依据可能会简化MAX计算。 与其尝试查找最多150,000行,不如将其分组为3行,然后计算MAX会更快?我的假设是正确的还是分组依据也代价高昂?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.