简体   繁体   English

替换雪花中的子查询

[英]Replacing subquery in Snowflake

I am new to Snowflake SQL and trying to migrate a code from Oracle SQL to Snowflake SQL. I am new to Snowflake SQL and trying to migrate a code from Oracle SQL to Snowflake SQL.

I am using a subquery in Oracle which is not supported in Snowflake and it gives the following err-r我在 Oracle 中使用了一个子查询,它在 Snowflake 中不受支持,它给出了以下 err-r

SQL compilation error: Unsupported subquery type cannot be evaluated. SQL 编译错误:无法评估不支持的子查询类型。

Original SQL Query -原装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')) ;

I reframed the query and it compiles and gives the same number of records, can anyone review the code and comment if its the same or if its in correct, kindly comment on same.我重新构建了查询,它编译并提供了相同数量的记录,任何人都可以查看代码并评论它是否相同或是否正确,请评论相同。

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"

Both queries are equivalent.这两个查询是等效的。 You can check that both return the same information by running the following query:您可以通过运行以下查询来检查两者是否返回相同的信息:

--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"

You can also try the following query which is also equivalent:您还可以尝试以下同样等效的查询:

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')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM