我的数据类似于:
Date UserID Visits
2012-01-01 2 5
...
我想输出一个拥有>的用户列表x访问至少y个日期(例如,从1月3日到1月10日至少3个日期有> 5次访问的用户)。
答案 0 :(得分:5)
试试这个:
SELECT SUB.UserId, COUNT(*) FROM (
SELECT VL.UserId FROM VisitLog VL
WHERE VL.Visits > 5
AND VL.Date BETWEEN '2014-01-03' AND '2014-01-10') SUB
GROUP BY SUB.UserId
HAVING COUNT(*) >= 3
子查询返回样本日期范围之间Visits > 5
个数的所有行。
然后计算结果,仅返回此条件至少匹配3次的用户。
您没有提供太多信息,但如果每个用户每个日期有多个记录,则使用此查询(完全相同的主体,只是按用户和日期求和的内部分组):
SELECT SUB.UserId, COUNT(*) FROM (
SELECT VL.UserId, VL.Date FROM VisitLog VL
WHERE VL.Date BETWEEN '2014-01-03' AND '2014-01-10'
GROUP BY VL.UserId, VL.Date
HAVING SUM(VL.Visits) > 5) SUB
GROUP BY SUB.UserId
HAVING COUNT(*) >= 3
答案 1 :(得分:0)
Select UserID,SUM(Visits) from TableOfData
where Date>@HereTheDate
group by UserID
having SUM(Visits)>5
答案 2 :(得分:0)
试试这个:
select * from users
where id in (
select UserID
from userVisits
where date between '2014-01-03' and '2014-01-10'
and visits >= 5
group by userid
having count(*) >= 3)