简体   繁体   English

如何通过将一个表中的 id 与 bigquery 中另一表的多个列中的 id 连接来获取记录?

[英]How to get records by joining id in one table with id in multiple columns in another table in bigquery?

I have two tables emp and prj.我有两张桌子emp和prj。 Columns and data types of tables are as follows Emp has EmpNo(INT), EmpITPrj(STR), EmpFinPrj(STR), EmpHRPrj(STR), EmpIntPrj(STR), EmpDate(STR) and prj has PrjID(STR), PrjStartDate(STR), PrjEndDate(STR).表的列和数据类型如下 Emp 有 EmpNo(INT), EmpITPrj(STR), EmpFinPrj(STR), EmpHRPrj(STR), EmpIntPrj(STR), EmpDate(STR) 和 prj 有 PrjID(STR), PrjStartDate( STR),项目结束日期(STR)。 The goal is to display all records that match the condition prj.PrjID is in one of the columns of emp.EmpITPrj, emp.EmpFinPrj, emp.EmpHRPrj or emp.EmpIntPrj AND emp.Date falls on or between the prj.PrjStartDate, prj.PrjEndDate.目标是显示所有符合条件的记录。 .PrjEndDate。


EmpNo   EmpITPrj    EmpFinPrj   EmpHRPrj    EmpIntPrj   Date
1         IT101       null        null         null     2019-09-01
2         null        Fin101      null         null     2001-06-05
3         null        Fin102      null         null     2005-11-25
4         null        null        null         Int501   2010-10-15
5         null        null        null         Int105   2019-01-10
6         null        null        null         Int444   2015-12-03
7         null        null        HR110        null     2012-08-19
8         IT101       null        null         null     2011-04-24
9         null        null        HR105        null     2005-02-09
10        IT102       null        null         null     2006-07-11



PrjID   PrjStartDate    PrjEndDate
Fin102  10/14/2005      12/14/2005
IT102   07/11/2006      10/30/2006
IT110   11/15/2010      01/31/2011
Int101  01/01/2015      03/31/2015
HR110   05/19/2012      08/19/2012
Int444  01/01/2015      03/01/2015

End Result:
EmpNo   EmpITPrj    EmpFinPrj   EmpHRPrj    EmpIntPrj   EmpDate     PrjID   PrjStartDate    PrjEndDate
3       null        Fin102      null        null        2005-11-25  Fin102  10/14/2005      12/14/2005
10      IT102       null        null        null        2006-07-11  IT102   07/11/2006      10/30/2006
7       null        null        HR110       null        2012-08-19  HR110   05/19/2012      08/19/2012

The current query I am trying doesn't yield any results.我正在尝试的当前查询没有产生任何结果。 Can anyone suggest how should I proceed with this query?谁能建议我应该如何继续这个查询?

SELECT
 *
FROM
  `bigquery-project-123.emp` AS t1,
  `bigquery-project-123.prj` t2
WHERE
  (t1.EmpITPrj = t2.PrjID
    OR t1.EmpFinPrj = t2.PrjID
    OR t1.EmpHRPrj = t2.PrjID
    OR t1.EmpIntPrj = t2.PrjID
  AND SAFE.PARSE_DATE("%Y%m%d", t1.Date) >= SAFE.PARSE_DATE("%Y%m%d",t2.PrjStartDate)
    AND SAFE.PARSE_DATE("%Y%m%d", t1.Date) <= SAFE.PARSE_DATE("%Y%m%d",t2.PrjEndDate)

Below is for BigQuery Standard SQL以下是 BigQuery 标准 SQL

#standardSQL
WITH `bigquery-project-123.emp` AS (
  SELECT 1 EmpNo, 'IT101' EmpITPrj, NULL EmpFinPrj, NULL EmpHRPrj, NULL EmpIntPrj, '2019-09-01' EmpDate union all
  SELECT 2, NULL, 'Fin101', NULL, NULL, '2001-06-05' union all
  SELECT 3, NULL, 'Fin102', NULL, NULL, '2005-11-25' union all
  SELECT 4, NULL, NULL, NULL, 'Int501', '2010-10-15' union all
  SELECT 5, NULL, NULL, NULL, 'Int105', '2019-01-10' union all
  SELECT 6, NULL, NULL, NULL, 'Int444', '2015-12-03' union all
  SELECT 7, NULL, NULL, 'HR110', NULL, '2012-08-19' union all
  SELECT 8, 'IT101', NULL, NULL, NULL, '2011-04-24' union all
  SELECT 9, NULL, NULL, 'HR105', NULL, '2005-02-09' union all
  SELECT 10, 'IT102', NULL, NULL, NULL, '2006-07-11' 
), `bigquery-project-123.prj` AS (
  SELECT 'Fin102' PrjID, '10/14/2005' PrjStartDate, '12/14/2005' PrjEndDate union all
  SELECT 'IT102', '07/11/2006', '10/30/2006' union all
  SELECT 'IT110', '11/15/2010', '01/31/2011' union all
  SELECT 'Int101', '01/01/2015', '03/31/2015' union all
  SELECT 'HR110', '05/19/2012', '08/19/2012' union all
  SELECT 'Int444', '01/01/2015', '03/01/2015' 
)
SELECT *
FROM `bigquery-project-123.emp` AS t1
JOIN `bigquery-project-123.prj` t2 
ON t2.PrjID IN (t1.EmpITPrj,t1.EmpFinPrj,t1.EmpHRPrj,t1.EmpIntPrj)
AND SAFE.PARSE_DATE("%Y-%m-%d", t1.EmpDate) BETWEEN 
SAFE.PARSE_DATE("%m/%d/%Y",t2.PrjStartDate) AND SAFE.PARSE_DATE("%m/%d/%Y",t2.PrjEndDate) 

with result结果

Row EmpNo   EmpITPrj    EmpFinPrj   EmpHRPrj    EmpIntPrj   EmpDate     PrjID   PrjStartDate    PrjEndDate   
1   3       null        Fin102      null        null        2005-11-25  Fin102  10/14/2005      12/14/2005   
2   7       null        null        HR110       null        2012-08-19  HR110   05/19/2012      08/19/2012   
3   10      IT102       null        null        null        2006-07-11  IT102   07/11/2006      10/30/2006   

group your or conditions into 1. I suggest to use newer way of joining a table.将您的or条件分组为 1。我建议使用更新的加入表格的方式。

SELECT
 *
FROM
  `bigquery-project-123.emp` AS t1
LEFT JOIN `bigquery-project-123.prj` t2
    ON (t1.EmpITPrj = t2.PrjID
        OR t1.EmpFinPrj = t2.PrjID
        OR t1.EmpHRPrj = t2.PrjID
        OR t1.EmpIntPrj = t2.PrjID)
WHERE 
  SAFE.PARSE_DATE("%Y%m%d", t1.Date) >= SAFE.PARSE_DATE("%Y%m%d",t2.PrjStartDate)
    AND SAFE.PARSE_DATE("%Y%m%d", t1.Date) <= SAFE.PARSE_DATE("%Y%m%d",t2.PrjEndDate)

暂无
暂无

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

相关问题 如何通过将一个表中的 id 与 bigquery 中另一个表中的多个列中的 id 连接来获取记录以获取大表? - How to get records by joining id in one table with id in multiple columns in another table in bigquery for huge tables? 如何从一个表中获取所有列,而从另一张表中仅获取具有ID的一列? -MySQL - How to get all columns from one table and only one column from another table with ID ? - MySql 将一个表中的多个列连接到另一个表中的单个列 - Joining multiple columns in one table to a single column in another table 如何为另一个表的每个ID向一个表中插入多行 - How to insert multiple rows into one table for each id of another table 如何获取一个表的自动增量ID并插入另一个表 - how to get autoincrement ID of one table and insert in another table 如何将具有相同 id 的多条记录的表重塑为每个 id 一条记录的表而不会丢失信息? - How to reshape a table having multiple records for the same id into a table with one record per id without losing information? 将一个表中的记录插入到另一个表中增加 id - Insert records from one table on another table incrementing id 根据一个表中的唯一 ID 连接多个表 - Joining Multiple tables based on a unique ID in one table 如何检查一个表到另一个表的ID? - How to check id of one table to another table? SQL将一个表的多条记录合并成另一表的多列 - SQL combine multiple records of one table into multiple columns of another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM