简体   繁体   English

SQL - GROUP BY 最大值

[英]SQL - GROUP BY max value

Note: I'm not sure if I gave this question the most leading title since I'm not sure on the correct approach towards this, but I couldn't find other examples anywhere since it's quite a specific query.注意:我不确定我是否给这个问题提供了最领先的标题,因为我不确定对此的正确方法,但我无法在任何地方找到其他示例,因为它是一个非常具体的查询。

So, I have a table "votes", which is filled with votes created by users (uniquely identified as a number in the user_id column) which correspond to relevent posts in another table (vote records "upvote" each relevent post within the user interface).因此,我有一个“投票”表,其中包含用户创建的投票(在user_id列中唯一标识为数字),这些投票对应于另一个表中的相关帖子(投票记录“upvote”用户界面中的每个相关帖子)。

I intend to sort these votes (by datetime) in order of latest vote created for each post ( post_id column), and as such, avoiding duplicate returned values of each post_id .我打算按照为每个帖子post_id列)创建的最新投票的顺序对这些投票(按日期时间)进行排序,因此,避免每个post_id 的重复返回值。

I input the following query:我输入以下查询:

SELECT id, user_id, post_id, created, MAX(created)
FROM votes
GROUP BY post_id, user_id
ORDER BY max(created) DESC

And get returned:并返回:

Table: votes

 id  |  user_id  |  post_id  |        created        |    MAX(created)
 ----+-----------+-----------+-----------------------+--------------------
 115 | 1         | 42        | 2014-07-03 23:08:31   | 2016-03-07 12:08:31
 ----+-----------+-----------+-----------------------+--------------------        
 237 | 2         | 101       | 2014-02-13 23:05:14   | 2016-03-05 23:05:14         
 ----+-----------+-----------+-----------------------+--------------------  
 431 | 7         | 944       | 2014-10-22 22:58:37   | 2016-03-03 19:58:37
 ----+-----------+-----------+-----------------------+--------------------
 255 | 15        | 101       | 2014-02-15 14:02:01   | 2016-02-01 23:05:14
 ----+-----------+-----------+-----------------------+--------------------
 ... | ...       | ...       | ...                   | ...

As you can see, there is a duplicate of the post_id "101".如您所见, post_id有重复的“101”。 The result of this query seems to sort by maximum created time for each user_id , showing duplicated post_id 's, eg there are two post_id column rows of "101", when I would only like to diplay the only post_id column value of "101" which has the maximum created time (MAX( created )).此查询的结果似乎按每个user_id的最大创建时间排序,显示重复的post_id ,例如有两个post_id列行“101”,当我只想显示“101”的唯一post_id列值时它具有最大创建时间(MAX(创建))。

The post_id and user_id columns seemingly must be grouped together, else if I just group by post_id I'm unable to sort by MAX( created ) since it won't return the max( created ) for each post_id. post_iduser_id列似乎必须组合在一起,否则如果我只是按post_id分组,我将无法按 MAX( created ) 排序,因为它不会返回每个 post_id 的 max( created )。

How do I remove these duplicated post_id values that don't return the maximum created time?如何删除这些不返回最大创建时间的重复post_id值?

What I'm after:我追求的是:

Table: votes

 id  |  user_id  |  post_id  |        created        |    MAX(created)
 ----+-----------+-----------+-----------------------+--------------------
 115 | 1         | 42        | 2014-07-03 23:08:31   | 2016-03-07 12:08:31
 ----+-----------+-----------+-----------------------+--------------------        
 237 | 2         | 101       | 2014-02-13 23:05:14   | 2016-03-05 23:05:14         
 ----+-----------+-----------+-----------------------+--------------------  
 431 | 7         | 944       | 2014-10-22 22:58:37   | 2016-03-03 19:58:37
 ----+-----------+-----------+-----------------------+--------------------
 ... | ...       | ...       | ...                   | ...

Assuming you only want the last vote for each post:假设您只想要每个帖子的最后一票:

SELECT  v.*
FROM    posts p
JOIN    votes v
ON      v.id =
        (
        SELECT  id
        FROM    votes vi
        WHERE   post_id = p.id
        ORDER BY
                created DESC
        LIMIT 1
        )

If you are looking for getting last user_id whom edited post_id , try group by post_id and ordering by time desc (or id if it is auto increment).如果您正在寻找编辑post_id最后一个user_id ,请尝试group by post_id并按时间 desc 排序(如果是自动递增,则为id )。

 SELECT tbl.* , GROUP_CONCAT('(',tbl.user_id,',',tbl.created,')') as myhistory FROM
   (SELECT id, user_id, post_id, created, MAX(created)
   FROM votes
   ORDER BY max(created) DESC
   ) as tbl
 GROUP BY tbl.post_id

If you need history for (user_id,time) you can use group_concat function as mentioned in code for myhistory column.如果您需要(user_id,time)历史记录(user_id,time)您可以使用myhistory列的代码中提到的group_concat函数。

SELECT maintable.*
FROM TABLE_NAME maintable
LEFT OUTER JOIN TABLE_NAME temporarytable
 ON maintable.GROUPING_BY_COLUMN = temporarytable.GROUPING_BY_COLUMN
 AND maintable.COLUMN_WHERE_THE_MAXIMUM_IS_NEEDED < temporarytable.COLUMN_WHERE_THE_MAXIMUM_IS_NEEDED
WHERE temporarytable.COLUMN_WHERE_THE_MAXIMUM_IS_NEEDED IS NULL
ORDER BY PRIMARY_KEY_COLUMN DESC
LIMIT 50;

An alternative way to get the maximum value from a group.从组中获取最大值的另一种方法。 This query does not require aggregation, as is the case with “GROUP BY”.此查询不需要聚合,就像“GROUP BY”一样。

In addition, when grouping using “GROUP BY”, each of the groups is sorted by primary key, which also takes a lot of time.另外,在使用“GROUP BY”进行分组时,每个组都是按主键排序的,这也需要很多时间。

My query compares the values of one table with another.我的查询将一个表的值与另一个表的值进行比较。 Until he can find nothing more.直到他再也找不到其他东西为止。 If nothing else is found, then this is the maximum.如果没有发现其他任何东西,那么这是最大值。

This query can help you save time getting the maximum value from the group.此查询可以帮助您节省从组中获取最大值的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM