首页 > 解决方案 > 如何为多个 Inner Join 编写 SQL 查询?

问题描述

样本记录:

    Row(user_id='KxGeqg5ccByhaZfQRI4Nnw', gender='male', year='2015', month='September', day='20', 
hour='16', weekday='Sunday', reviewClass='place love back', business_id='S75Lf-Q3bCCckQ3w7mSN2g', 
business_name='Notorious Burgers', city='Scottsdale', categories='Nightlife, American (New), Burgers, 
Comfort Food, Cocktail Bars, Restaurants, Food, Bars, American (Traditional)', user_funny='1', 
review_sentiment='Positive', friend_id='my4q3Sy6Ei45V58N2l8VGw')

这张表有超过一亿条记录。我的 SQL 查询正在执行以下操作:

Select the most occurring review_sentiment among the friends (friend_id) and the most occurring gender among friends of a particular user visiting a specific business

friend_id is eventually a user_id

示例场景:

我想要以下输出:

**user_id | business_id | friend_common_sentiment | mostCommonGender | .... otherCols**

user_id_1 | business_id_1 | positive | male | .... otherCols
user_id_1 | business_id_2 | negative | female | .... otherCols
user_id_1 | business_id_3 | negative | female | .... otherCols

这是我为此编写的一个简单查询pyspark

SELECT user_id, gender, year, month, day, hour, weekday, reviewClass, business_id, business_name, city, 
categories, user_funny, review_sentiment FROM events1 GROUP BY user_id, friend_id, business_id ORDER BY 
COUNT(review_sentiment DESC LIMIT 1

此查询不会给出预期的结果,但我不确定如何将 INNER-JOIN 完全融入其中?

标签: mysqlsqlpysparkpyspark-sql

解决方案


人类确实使数据结构变得困难。但是让我们把它分解成几个步骤,

  1. 您需要自行加入才能获取朋友的数据
  2. 获得朋友的数据后,执行聚合函数以获取每个可能值的计数,按用户和企业分组
  3. 子查询上述内容,以便根据计数在值之间做出决定。

我只是将您的表称为“标签”,因此连接如下,遗憾的是就像在现实生活中一样,我们不能假设每个人都有朋友,并且由于您没有指定排除永远孤独的人群,我们需要使用左连接来保持用户没有朋友。

From tags as user
left outer join tags as friends on user.friend_id = friends.user_id
    and friends.business_id = user.business_id

接下来,您必须弄清楚给定用户和业务组合最常见的性别/评论是什么。这就是数据结构真正让我们大吃一惊的地方,我们可以通过一些巧妙的窗口函数一步完成,但我希望这个答案易于理解,所以我将使用子查询和案例陈述。为简单起见,我假设为二元性别,但根据您应用的唤醒级别,您可以遵循相同的模式来获取其他性别。

select user.user_id, user.business_id
, sum(case when friends.gender = 'Male' then 1 else 0 end) as MaleFriends
, sum(case when friends.gender = 'Female' then 1 else 0 end) as FemaleFriends
, sum(case when friends.review_sentiment = 'Positive' then 1 else 0 end) as FriendsPositive
, sum(case when friends.review_sentiment = 'Negative' then 1 else 0 end) as FriendsNegative
From tags as user
left outer join tags as friends on user.friend_id = friends.user_id
  and friends.business_id = user.business_id
where user.business_id = <<your business id here>>
group by user.user_id, user.business_id

现在我们只需要从子查询中获取数据并做出一些决定,您可能想要添加一些额外的选项,例如您可能想要添加选项以防没有朋友,或者朋友在性别/情感之间平均分配. 与以下相同的模式,但有额外的值可供选择。

select user_id
, business_id
, case when MaleFriends > than FemaleFriends then 'Male' else 'Female' as MostCommonGender
, case when FriendsPositive > FriendsNegative then 'Positive' else 'Negative' as MostCommonSentiment
from (    select user.user_id, user.business_id
, sum(case when friends.gender = 'Male' then 1 else 0 end) as MaleFriends
, sum(case when friends.gender = 'Female' then 1 else 0 end) as FemaleFriends
, sum(case when friends.review_sentiment = 'Positive' then 1 else 0 end) as FriendsPositive
, sum(case when friends.review_sentiment = 'Negative' then 1 else 0 end) as FriendsNegative
From tags as user
left outer join tags as friends on user.friend_id = friends.user_id
  and friends.business_id = user.business_id
where user.business_id = <<your business id here>>
group by user.user_id, user.business_id) as a

这为您提供了要遵循的步骤,并希望能清楚地解释它们的工作原理。祝你好运!


推荐阅读