我对这个查询有一个心理障碍,我试图返回最大日期和最大时间,并根据身份进行排序。如果有人能在这种类型的查询中添加一双眼睛,那将是非常感激的:
数据集
Identity, Date, Time, Website
10, 5/10/15, 1, google.com
10, 5/10/15, 3, google.com
10, 5/10/15, 10, google.com
25, 5/11/15, 1, yahoo.com
25, 5/11/15, 15, yahoo.com预期结果
10, 5/10/15, 10, google.com
25, 5/11/15, 15, yahoo.com当前查询
SELECT DISTINCT *, MAX(datetime) as maxdate, MAX(time), identity
FROM identity_track
GROUP BY identity
ORDER BY maxdate DESC发布于 2015-05-14 01:54:14
像这样吗?
select identity, max(date), max(time), website
from identity_track
group by website;这里的演示:http://sqlfiddle.com/#!9/5cadf/1
你可以按你想要的任何字段来订购。
此外,您发布的预期输出与您试图做的事情不一致。
编辑
基于附加信息的更新查询。
select t.identity, t.date, max(t.time), t.website
from t
inner join
(select identity, website, max(date) d
from t
group by identity, website) q
on t.identity = q.identity
and t.website = q.website
and q.d = t.date
group by t.identity, t.website, t.date这个应该给你用户的身份,他访问的页面,他最后一次访问该页面,以及他在上次访问中花费的最多时间。
发布于 2015-05-14 02:13:09
不要假设身份的所有记录都在同一天,例如,如果实体的时间分别为1/1/15和1/2/15 2 5pm,那么就会得到1/2/15的下午1/2/15,这是错误的。
我总是把时间和日期合并起来,但是如果你不能这样做的话:
select t.identity, t.website, MAX(t.time)
FROM t
INNER JOIN
(
select identity, max(date) as max_date
from t
group by identity;
) x
ON t.identity = x.identity
AND t.date = x.max_date
group by t.identity, t.website首先,我们得到了每个站点的最大日期。那一天,最大的时间。
希望这能有所帮助。
https://stackoverflow.com/questions/30227886
复制相似问题