查看: 1041| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

SQL 的一个小问题求助

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
  1. SELECT COUNT(CASE WHEN companies.permalink IS NOT NULL AND acquisitions.company_permalink IS NULL
  2. THEN companies.permalink ELSE NULL END) AS companies_only,
  3. COUNT(CASE WHEN companies.permalink IS NOT NULL AND acquisitions.company_permalink IS NOT NULL
  4. THEN companies.permalink ELSE NULL END) AS both_tables,
  5. COUNT(CASE WHEN companies.permalink IS NULL AND acquisitions.company_permalink IS NOT NULL
  6. THEN acquisitions.company_permalink ELSE NULL END) AS acquisitions_only
  7. FROM tutorial.crunchbase_companies companies
  8. FULL JOIN tutorial.crunchbase_acquisitions acquisitions
  9. ON companies.permalink = acquisitions.company_permalink
复制代码


最近在用MODE复习SQL,有一个小问题想要请教一下大家。

这段代码是用于区分data source,后面有一段注释是这样说的:
One important thing to keep in mind is that you must count from the crunchbase_acquisitions table in order to get unmatched rows in that table—if you were to count companies.permalink as in the first two columns, you would get a result of 0 in the third column because it would be counting up a bunch of null values.

我到这里有一点似懂非懂,大概知道因为company肯定是数据比较多的一个表,也就是说,acquisitions是它的子集,如果取它为NULL,那么结果可能就是0了。但是还是有一点模糊,哪位朋友可以帮忙解释一下吗?
网址如下:网址如下:https://community.modeanalytics. ... ql-full-outer-join/

非常感谢!!



上一篇:以梦为马,重整出发——记录刷题 自我打气贴
下一篇:求删帖
🔗
gjw931110 2018-9-15 09:27:48 | 只看该作者
全局:
这里用了,full join,我们试想一下如果不用full join的话,会是什么情况?
permalink 是 crunchbase_companies as companies table 的 primary key,所以这一列本身在进行任何join前不会存在null值。
代码第三个when:CASE WHEN companies.permalink IS NULL AND acquisitions.company_permalink IS NOT NULL
THEN acquisitions.company_permalink
companies 这个table的permalink什么时候才可能出现null值呢,无非是 另外一个表join过来,带来了一些没有match上的行,在permalink列填充的null值。
文中说,in order to get unmatched rows from acquisitions table 如果你不用right join 或者 full join 是得不到这个的。

简述,这段话其实没有任何什么意义,平时sql 会很少用full join,这只是一个非常生硬的例子。
如果你想得到intersection(matched rows) 你用join
如果你想得到unmatched rows from left table 你用left join 再加上where join 过来的某一列的column name is null
如果你想得到unmacthed rows from right table 你用right join 再加上where join 过来的某一列的column name is null
如果你想得到both unmatched rows 你用full join 再加上where 分别两个table的某一列是null 用or 连接

评分

参与人数 1大米 +2 收起 理由
rubyzhao + 2 thanks for your help!

查看全部评分

回复

使用道具 举报

🔗
 楼主| rubyzhao 2018-9-15 11:33:31 | 只看该作者
全局:
gjw931110 发表于 2018-9-15 09:27
这里用了,full join,我们试想一下如果不用full join的话,会是什么情况?
permalink 是 crunchbase_comp ...

谢谢回复!我明白了你说的 但是if you were to count companies.permalink as in the first two columns, you would get a result of 0 in the third column because it would be counting up a bunch of null values这句话还是不太明白,可以再帮我分析一下这句话吗?
现在的运行结果是这样的:
        25682        1673           5741
回复

使用道具 举报

🔗
gjw931110 2018-9-15 12:19:55 | 只看该作者
全局:
rubyzhao 发表于 2018-9-15 11:33
谢谢回复!我明白了你说的 但是if you were to count companies.permalink as in the first two columns, ...

sry 我无法理解。我觉得不需要纠结
回复

使用道具 举报

🔗
stellari 2018-9-17 14:57:40 | 只看该作者
全局:
full join的结果可能是这个样子(列名我简写了):
  1. comp.pm  | acqs.cp_pm
  2. abc          | NULL
  3. def          | def
  4. NULL       | ghi
  5. NULL     | jkl
复制代码


那段话的意思是:在用COUNT得到companies_only和both_tables这两个指标时,你可以用company.permalink这一列作为COUNT的基准,因为company.permalink列在满足这两个指标的那些行上(上例中前两行)的值一定不是NULL,对于这些非NULL值进行COUNT会得到预期的结果;但是在计算acquisitions_only这个指标时,则应该对acquisitions.company_permalink进行COUNT,因为此时company.permalink列满足该指标的行(第3, 4行)一定是NULL。对于company.permalink中的这些NULL值进行COUNT必然会得到0。

评分

参与人数 1大米 +2 收起 理由
rubyzhao + 2 谢谢你!

查看全部评分

回复

使用道具 举报

🔗
 楼主| rubyzhao 2018-9-29 06:15:23 | 只看该作者
全局:
stellari 发表于 2018-9-17 14:57
full join的结果可能是这个样子(列名我简写了):
[mw_shl_code=bash,true]comp.pm  | acqs.cp_pm
abc   ...

谢谢你!我明白自己哪里的问题了!
回复

使用道具 举报

🔗
 楼主| rubyzhao 2018-9-29 06:16:37 | 只看该作者
全局:
gjw931110 发表于 2018-9-15 12:19
sry 我无法理解。我觉得不需要纠结

有一个地方我想错了,现在想通了,也谢谢你啦!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表