替换雪花中的子查询

Posted

技术标签:

【中文标题】替换雪花中的子查询【英文标题】:Replacing subquery in Snowflake 【发布时间】:2021-05-21 10:57:15 【问题描述】:

我是 Snowflake SQL 的新手,正在尝试将代码从 Oracle SQL 迁移到 Snowflake SQL。

我在 Oracle 中使用了一个 Snowflake 不支持的子查询,它给出了以下 err-r

SQL 编译错误:无法评估不受支持的子查询类型。

原始 SQL 查询 -

select p."EMPLOYEE#" as Employee_ID,
 (select o."POSITION_NO" from ah_occupancy o where o."EMPLOYEE_NO" = p."EMPLOYEE#" and o."JOB_NO" = p."JOB#" and p."WORKDATE" between o."PORTION_START" and o."PORTION_END") as Position_ID,
    j."COMPANY_CODE",
    date(p."WORKDATE") as Work_Date,
    p."UNIT" as Calculated_Quantity,
    p."PAYCODE"
from
    "ODS"."HRIS"."PEPAYTRAN" p, job j
where p."EMPLOYEE#" =j."EMPLOYEE#"
    and p."JOB#" = j."JOB#"
    and date( p."WORKDATE") between '2021-02-04' and '2021-02-10'-->='11-FEB-2021'
    and p."ORIGIN" not in ('RC101','FC801','WK8276')
    and p."PAYCODE" not in ('GPPL', 'CLAMS')
    and p."PAYCODE" not like 'JK%'
    and p."TP" >= '01-JAN-2021'
    and nvl(p."BATCH#",' ') not in ('MUHTSL','MUHTS','ICWTS','RDOAC','NU011','1')) ;

我重新构建了查询,它编译并提供了相同数量的记录,任何人都可以查看代码并评论是否相同或正确,请评论相同。

select p."EMPLOYEE#" as Employee_ID,
    o.position_no as Position_ID,
    j."COMPANY_CODE",
    date(p."WORKDATE") as Work_Date,
    p."UNIT" as Calculated_Quantity,
    p."PAYCODE"
from
    "ODS"."HRIS"."PEPAYTRAN" p,  "ODS"."HRIS"."JOB" j, "ODS"."HRIS"."AH_OCCUPANCY" o
where p."EMPLOYEE#" =j."EMPLOYEE#"
    and p."JOB#" = j."JOB#"
    and date( p."WORKDATE") between '2021-02-04' and '2021-02-10'-->='11-FEB-2021'
    and p."ORIGIN" not in ('RC101','FC801','WK8276')
    and p."PAYCODE" not in ('GPPL', 'CLAMS')
    and p."PAYCODE" not like 'JK%'
    and p."TP" >= '01-JAN-2021'
    and nvl(p."BATCH#",' ') not in ('MUHTSL','MUHTS','ICWTS','RDOAC','NU011','1')
    and o."EMPLOYEE_NO" = p."EMPLOYEE#" and o."JOB_NO" = p."JOB#" and p."WORKDATE" between o."PORTION_START" and o."PORTION_END"

【问题讨论】:

如果您的新版本查询运行并产生正确的结果,您面临什么问题,您要问什么问题? 我希望有人检查这两个代码并检查技术上是否相同。 如果它们都产生相同的结果,那么它们在技术上是相同的 【参考方案1】:

这两个查询是等效的。您可以通过运行以下查询来检查两者是否返回相同的信息:

--original query
select 
   p."EMPLOYEE#" as Employee_ID,
   (select 
      o."POSITION_NO" 
   from 
      ah_occupancy o 
   where 
      o."EMPLOYEE_NO" = p."EMPLOYEE#" 
      and o."JOB_NO" = p."JOB#" 
      and p."WORKDATE" between o."PORTION_START" and o."PORTION_END"
   ) as Position_ID,
   j."COMPANY_CODE",
   date(p."WORKDATE") as Work_Date,
   p."UNIT" as Calculated_Quantity,
   p."PAYCODE"
from
    "ODS"."HRIS"."PEPAYTRAN" p, 
    job j
where 
   p."EMPLOYEE#" =j."EMPLOYEE#"
   and p."JOB#" = j."JOB#"
   and date( p."WORKDATE") between '2021-02-04' and '2021-02-10'-->='11-FEB-2021'
   and p."ORIGIN" not in ('RC101','FC801','WK8276')
   and p."PAYCODE" not in ('GPPL', 'CLAMS')
   and p."PAYCODE" not like 'JK%'
   and p."TP" >= '01-JAN-2021'
   and nvl(p."BATCH#",' ') not in ('MUHTSL','MUHTS','ICWTS','RDOAC','NU011','1')) ;
minus
--new query
select 
   p."EMPLOYEE#" as Employee_ID,
   o.position_no as Position_ID,
   j."COMPANY_CODE",
   date(p."WORKDATE") as Work_Date,
   p."UNIT" as Calculated_Quantity,
   p."PAYCODE"
from
    "ODS"."HRIS"."PEPAYTRAN" p,  
    "ODS"."HRIS"."JOB" j, 
    "ODS"."HRIS"."AH_OCCUPANCY" o
where 
   p."EMPLOYEE#" =j."EMPLOYEE#"
   and p."JOB#" = j."JOB#"
   and date( p."WORKDATE") between '2021-02-04' and '2021-02-10'-->='11-FEB-2021'
   and p."ORIGIN" not in ('RC101','FC801','WK8276')
   and p."PAYCODE" not in ('GPPL', 'CLAMS')
   and p."PAYCODE" not like 'JK%'
   and p."TP" >= '01-JAN-2021'
   and nvl(p."BATCH#",' ') not in ('MUHTSL','MUHTS','ICWTS','RDOAC','NU011','1')
   and o."EMPLOYEE_NO" = p."EMPLOYEE#" 
   and o."JOB_NO" = p."JOB#" 
   and p."WORKDATE" between o."PORTION_START" and o."PORTION_END"

您也可以尝试以下同样等价的查询:

select 
   p."EMPLOYEE#" as Employee_ID,
   o.position_no as Position_ID,
   j."COMPANY_CODE",
   date(p."WORKDATE") as Work_Date,
   p."UNIT" as Calculated_Quantity,
   p."PAYCODE"
from
    "ODS"."HRIS"."PEPAYTRAN" p
    inner join  "ODS"."HRIS"."JOB" j
       on (p."EMPLOYEE#" =j."EMPLOYEE#"
           and p."JOB#" = j."JOB#")
    inner join "ODS"."HRIS"."AH_OCCUPANCY" o
       on (o."EMPLOYEE_NO" = p."EMPLOYEE#" 
           and o."JOB_NO" = p."JOB#" 
           and p."WORKDATE" between o."PORTION_START" and o."PORTION_END"  )
where 
   date( p."WORKDATE") between '2021-02-04' and '2021-02-10'-->='11-FEB-2021'
   and p."ORIGIN" not in ('RC101','FC801','WK8276')
   and p."PAYCODE" not in ('GPPL', 'CLAMS')
   and p."PAYCODE" not like 'JK%'
   and p."TP" >= '01-JAN-2021'
   and nvl(p."BATCH#",' ') not in ('MUHTSL','MUHTS','ICWTS','RDOAC','NU011','1')

【讨论】:

以上是关于替换雪花中的子查询的主要内容,如果未能解决你的问题,请参考以下文章

以数组为参数的雪花函数因不支持的子查询错误而失败

使用函数时雪花不支持的子查询

雪花:无法评估不受支持的子查询类型

雪花标量 UDF 返回 无法评估不支持的子查询类型

雪花不支持的子查询类型无法在 UDF 标量中评估

雪花 - 检查模式并返回该模式中的子字符串