[英]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.