我有两个表emp和prj。表的列和数据类型如下:Emp具有EmpNo(INT),EmpITPrj(STR),EmpFinPrj(STR),EmpHRPrj(STR),EmpIntPrj(STR),EmpDate(STR)和 prj具有PrjID(STR),PrjStartDate(STR),PrjEndDate(STR)。 目标是显示与条件prj.PrjID匹配的所有记录在emp.EmpITPrj,emp.EmpFinPrj,emp.EmpHRPrj或emp.EmpIntPrj的列之一中,并且emp.Date落在prj.PrjStartDate,prj上或之间.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
我正在尝试的当前查询不会产生任何结果。谁能建议我该如何进行此查询?
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)
答案 0 :(得分:1)
以下是用于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)
有结果
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