我有一张表,里面有我们网站访问者的信息。
所以它看起来像
VISITOR_ID EVENT Date Rank
(我已经按事件划分了它们的排名)
Visitor_id Event Date rank
1 visit 1/1/14 1
1 purchase 1/2/14 2
1 visit 1/3/14 3
1 visit 1/4/14 4
1 purchase 1/5/14 5
1 visit 1/6/14 6
1 visit 1/7/14 7
1 visit 1/8/14 8
1 purchase 1/9/14 9
我想找到所有的最小和最大访问日期(因此每个用户每次购买前的第一次和最后一次访问)每次购买之前,所以结果应该有日期
visitor mindate maxdate
1 1/1/2014 1/1/2014 (in this scenario there was only one visit before a purchase)
1 1/3/2014 1/4/2014 ( 2 OR MORE VISITS BEFORE A PURCHASE)
1 1/6/2014 1/8/2014
这只是一个访问者的例子。这张桌子有大约一百万的访客。请帮忙。
您需要将访问分组。你可以用一个简单的技巧做到这一点。如果您枚举访问的行并从排名中减去,则每个组都将有一个常数值。所以,这很容易:
select visitor, min(date) as mindate, max(date) as maxdate
from (select t.*, row_number() over (partition by visitor order by rank) as v_rank
from table t
where event = 'Visit'
) t
group by visitor, (rank - v_rank);
这里有另一种选择:
SELECT
visitor_id
, MIN(Date)
, MAX(Date)
FROM
visitors v
INNER JOIN
(SELECT
rank
FROM visitors
WHERE Event = 'purchase' AND v.visitor_id = visitors.visitor_id) purchases
WHERE v.rank < purchases.rank
GROUP BY v.visitor_id, v.rank;