该查询确定每天导致匹配结果的右挥动百分比

时间:2019-06-11 05:40:22

标签: sql join

如何查找每天导致匹配的右滑动百分比。当用户A向右滑动用户B,而用户B向右滑动用户A时,会发生匹配。

以下是示例数据:

swiping_user_id   swiped_on_user_id      swipe_at            swipe_type
1 1958315            7247259         2015-03-15 00:01:05      right
2 5823050            8732832         2014-06-10 05:12:05      left
3 7948392            6767291         2015-08-10 12:45:01      right

2 个答案:

答案 0 :(得分:0)

此查询将为您提供导致匹配的滑动次数:

select count(*) matchedSwipes
from SwipeTable st1
join SwipeTable st2 on 
st1.swiping_user_id = st2.swiped_on_user_id and
st2.swiping_user_id = st1.swiped_on_user_id and
st1.swipe_type = st2.swipe_type
where s1.swipe_type = 'right'

然后只需获取刷卡总数,然后将匹配的刷卡次数除以该总数即可。

答案 1 :(得分:0)

如果需要该比例,则可以执行以下操作:

select count(*) as num_right_swipes,
       count(st2.swipe_type) as num_matches,
       count(st2.swipe_type) * 1.0 / count(*) as ratio
from SwipeTable st1 left join
     SwipeTable st2
     on st2.swiping_user_id = st1.swiped_on_user_id and
        st2.swiped_user_id = st1.swiping_on_user_id and
        st2.swipe_type = st1.swipe_type
where s2.swipe_type = 'right';

这假定刷卡是唯一的(即,从人A到人B两次没有“向右”刷卡)。如果不是这种情况,您应该问一个 new 问题,并说明如何处理重复项。