我有一个表有user_id,item_id和last_date作为列。最后一列类型是在datetime中。
我需要通过将列user_id和item_id分组来检索last_date,其中type_id就像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
以下是一种方式 -
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
使用相关子查询
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列进行分组,然后获取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;