繁体   English   中英

如何为多个内部联接编写 SQL 查询?

[英]How to write a SQL query for multiple Inner Join?

样本记录:

    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')

该表有超过 1 亿条记录。 我的 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

示例场景:

  • 一位用户
  • 已访问 4 家企业
  • 有10个朋友
  • 其中 5 位朋友访问了第 1 家和第 2 家公司,而其他 5 位只访问了第 3 家公司,没有人访问过第 4 家公司
  • 现在,对于业务 1 和 2,这 5 个朋友对 B1 的积极情绪多于消极情绪,对 B2 的 -ve 情绪多于 +ve,对 B3 的所有 -ve 情绪

我想要以下输出:

**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 放入其中?

人类是不是那种数据结构让事情变得困难了。 但是让我们把它分解成几个步骤,

  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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM