MySQL按两列分组,其中条件和第三列的最大日期

时间:2019-02-18 08:06:34

标签: mysql sql group-by max aggregate-functions

我有一个表,其中包含user_id,item_id和last_date作为列。最后一列类型为 datetime

我需要通过将 user_id item_id 列分组来检索 last_date ,条件是 type_id 类似于< strong> foo 并且非常重要,我需要在结果中包含所有列,因为在mytable中有更多列。

这是mytable结构:

user_id  item_id      last_date         type_id
129678      2     2019-02-17 11:00:09     foo
129678      1     2019-02-17 11:00:15     foo
129678      2     2019-02-17 11:00:10     bar
129678      2     2019-02-17 11:00:10     bar
129678      2     2019-02-17 11:00:11     foo
129678      2     2019-02-17 11:00:15     foo
129678      1     2019-02-17 11:00:09     foo
129678      2     2019-02-17 11:00:14     bar
129678      2     2019-02-17 11:00:08     bar
129678      2     2019-02-17 11:00:11     foo
129678      2     2019-02-17 11:00:14     bar
129678      2     2019-02-17 11:00:10     foo
12a0d8      3     2019-02-17 11:00:08     foo
12a0d8      2     2019-02-17 11:00:12     foo
12a0d8      3     2019-02-17 11:00:08     bar
12a0d8      3     2019-02-17 11:00:12     bar
12a0d8      1     2019-02-17 11:00:10     foo
12a0d8      1     2019-02-17 11:00:11     bar
12a0d8      3     2019-02-17 11:00:14     foo
12a0d8      3     2019-02-17 11:00:12     foo
18ae98      2     2019-02-17 11:00:12     foo
18ae98      3     2019-02-17 11:00:07     bar
18ae98      1     2019-02-17 11:00:13     bar
18ae98      1     2019-02-17 11:00:14     foo
18ae98      2     2019-02-17 11:00:09     foo
18ae98      2     2019-02-17 11:00:13     foo
18ae98      3     2019-02-17 11:00:08     foo
18ae98      1     2019-02-17 11:00:12     foo
18ae98      3     2019-02-17 11:00:10     foo
18ae98      1     2019-02-17 11:00:12     foo

这是我为此目的尝试的查询:

SELECT * FROM mytable
  WHERE last_date IN (
    SELECT MAX(last_date)
    FROM mytable
    WHERE type_id LIKE 'foo'
    GROUP BY user_id, item_id
  )

所需结果:

user_id  item_id      last_date         type_id
129678      1     2019-02-17 11:00:15     foo
129678      2     2019-02-17 11:00:15     foo
12a0d8      1     2019-02-17 11:00:10     foo
12a0d8      2     2019-02-17 11:00:12     foo
12a0d8      3     2019-02-17 11:00:14     foo
18ae98      1     2019-02-17 11:00:14     foo
18ae98      2     2019-02-17 11:00:13     foo
18ae98      3     2019-02-17 11:00:10     foo

但是结果很奇怪!例如,返回具有相同值的 user_id 多个 item_id ...

编辑:

如果我使用此查询可以正常工作,但必须指定一个间隔日期。

SELECT * FROM mytable
WHERE type_id LIKE 'foo'
AND last_date > date_sub(curdate(), interval 2 day)
GROUP BY user_id, item_id

3 个答案:

答案 0 :(得分:1)

下面是一种方法-

    SELECT 
from       MyTable t1 
INNER JOIN 
           ( 
                    SELECT   user_id, 
                             item_id, 
                             max(last_date) AS lastdate 
                    FROM     MyTable 
                    WHERE    type_id='foo' 
                    GROUP BY user_id, 
                             item_id) t2 
ON         t1.user_id=t2.user_id 
AND        t1.item_id=t2.item_id 
AND        t1.last_date=t2.lastdate

答案 1 :(得分:0)

使用相关子查询

   SELECT t1.* FROM mytable t1
   WHERE last_date = (
    SELECT MAX(last_date)
    FROM mytable t2
    WHERE t1.user_id=t2.user_id and t1.itemid=t2.itemid    
    and type_id LIKE 'foo'

  )

答案 2 :(得分:0)

您只需将您关注的2列分组,然后获取max(last_date)。您以前使用的子查询不会限制max(last_date)的上限,因为多个user_id和item_id组合可以应用于同一日期。

下面是用示例数据进行写操作的示例,它产生了预期的结果,而无需编写额外的嵌套查询。

declare @t table (
user_id nvarchar(15),
item_id int,
last_date datetime2,
type_id nvarchar(3)
);

insert into  @t (user_id, item_id, last_date, type_id)
values
('129678',      2,     '2019-02-17 11:00:09',     'foo'),
('129678',      1,     '2019-02-17 11:00:15',     'foo'),
('129678',      2,     '2019-02-17 11:00:10',     'bar'),
('129678',      2,     '2019-02-17 11:00:10',     'bar'),
('129678',      2,     '2019-02-17 11:00:11',     'foo'),
('129678',      2,     '2019-02-17 11:00:15',     'foo'),
('129678',      1,     '2019-02-17 11:00:09',     'foo'),
('129678',      2,     '2019-02-17 11:00:14',     'bar'),
('129678',      2,     '2019-02-17 11:00:08',     'bar'),
('129678',      2,     '2019-02-17 11:00:11',     'foo'),
('129678',      2,     '2019-02-17 11:00:14',     'bar'),
('129678',      2,     '2019-02-17 11:00:10',     'foo'),
('12a0d8',      3,     '2019-02-17 11:00:08',     'foo'),
('12a0d8',      2,     '2019-02-17 11:00:12',     'foo'),
('12a0d8',      3,     '2019-02-17 11:00:08',     'bar'),
('12a0d8',      3,     '2019-02-17 11:00:12',     'bar'),
('12a0d8',      1,     '2019-02-17 11:00:10',     'foo'),
('12a0d8',      1,     '2019-02-17 11:00:11',     'bar'),
('12a0d8',      3,     '2019-02-17 11:00:14',     'foo'),
('12a0d8',      3,     '2019-02-17 11:00:12',     'foo'),
('18ae98',      2,     '2019-02-17 11:00:12',     'foo'),
('18ae98',      3,     '2019-02-17 11:00:07',     'bar'),
('18ae98',      1,     '2019-02-17 11:00:13',     'bar'),
('18ae98',      1,     '2019-02-17 11:00:14',     'foo'),
('18ae98',      2,     '2019-02-17 11:00:09',     'foo'),
('18ae98',      2,     '2019-02-17 11:00:13',     'foo'),
('18ae98',      3,     '2019-02-17 11:00:08',     'foo'),
('18ae98',      1,     '2019-02-17 11:00:12',     'foo'),
('18ae98',      3,     '2019-02-17 11:00:10',     'foo'),
('18ae98',      1,     '2019-02-17 11:00:12',     'foo');

select * from (
select USER_ID, item_id, MAX(last_date) as last_date, type_id 
from @t where type_id = 'foo' group by user_id, item_id, type_id
) x
order by user_id, item_id;