【问题标题】:How to count total crossover from two tables each with specific conditions如何计算两个表的总交叉数,每个表都有特定条件
【发布时间】:2020-10-07 20:08:13
【问题描述】:

我正在处理数据集中的两个表。我们称第一个为“Demographic_Info”,另一个为“Study_Info”。这两个表都有一个 Subject_ID 列。如何运行查询以返回所有 Subject_ID,其中 Sex = Male(来自 Demographic_Info)以及 Study Case = Case(来自 Study_Info)?

这是一个内部连接吗?我需要做一个组合表吗?

我只是不知道要使用什么功能。我知道如何在每个表中单独选择这些条件,但不知道如何相互运行它们。

【问题讨论】:

  • 请以表格文本的形式提供示例数据和所需结果。

标签: sql google-bigquery


【解决方案1】:

是的,您需要进行内部连接,然后使用 where 子句过滤两个表。

select
  s.Subject_ID
from `Study_info` s
inner join `Demographic_info` d on s.Subject_ID = d.Subject_ID
where d.Sex = 'Male'
  and s.Study_Case = 'Case' -- Unclear from your question about the actual field name

别名 sd 将有助于组织每个字段来自哪个表(或者如果两个表中出现相同的字段)。

同样,您可以先过滤,然后执行连接。

with study as (select * from `Study_info` where Study_Case = 'Case'),
     demographics as (select * from `Demographic_info` where Sex = 'Male')
select s.Subject_ID
from study s
inner join demographics d on s.Subject_ID = d.Subject_ID

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-29
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多