SQL NOT EXISTS 子查询未按预期运行

SQL NOT EXISTS sub query not functioning as expected

我在使用查询根据文章的提前期过滤结果时遇到问题。令我困惑的部分是其中有一个子部分,我需要根据交货时间和原产国过滤某些文章。我正在尝试使用 WHERE 一侧的 NOT EXISTS 语句来执行此操作,但结果似乎不是我所期望的。例如,所有原产国为洪都拉斯且提前期为 90 的文章不应该 return 任何 so_conf_del_date 在 2019 年 2 月 15 日之后的结果...但是在结果中我得到很多3 月份的日期结果应该排除在我能看到的范围之外。以为我会对它有不同的看法,因为我真的不使用 not exists a ton。谢谢。

更新:从 NOT EXISTS 更改为 NOT IN 并更新了相关性。还要注意子查询完全按照它应该的方式执行,所以不确定为什么我得到的结果超出范围

查询已更新:

/* What to do about multiple COO's in ONE RANGE? ...join on order ORDER_ITEM.SEASONAL_INDICATOR? */
/* Use CASE WHEN in subquery to JOIN outerquery on vw_order_item.seasonal_indicator = OneRange_NAM.SEASON */
/* Assumption is being made that 'seasonal indicator' in vw_order_item would align with 'season' and thus COO in OneRange */
/* Added Date/LeadTime Parameters */
/* Use 'Planned Delivery Time'...this includes lead time and delivery time? */
/* Right join to ONE RANGE */
/* ARUN has no fully unallocated quantities - YES IT DOES */
/* 11/12/18 - replaced RDD with CDD */

SELECT kd.business_segment_desc, 
       q0.plant_code,
       q0.req_cat AS 'requirement_category',
       m.[department], 
       q0.commission_code_id,
       aa.LeadTime,
       q0.so_conf_del_date, 
       q0.valid_to_date,
       q0.sales_order_number, 
       q0.sales_order_item_number,
       q0.sold_to, 
       q0.bill_to,
       m.working_number,
       q0.material,
       m.[description],
       q0.open_quantity,
       q0.allocated_quantity,
       q0.unallocated_quantity,
       q0.percent_unallocated,
       aa.ActivationStatus AS 'global status',
       m.ib_lock,
       o.country_of_origin

FROM   pdx_sap_user..vw_mm_material m
JOIN   pdx_sap_user..vw_kd_business_segment kd ON m.business_segment_code = kd.business_segment_code
JOIN   asagdwpdx_prod..ArticleNumbers aa ON m.material = aa.ArticleNumber
JOIN   adi_user_maintained..VW_ONERange_NAM o ON     m.material = o.article 
                                              AND    aa.ArticleNumber = o.Article
JOIN

(SELECT i.plant_code, 
       h.commission_code_id, 
       a.so_conf_del_date,
       h.valid_to_date,
       i.sales_order_number, 
       i.sales_order_item_number,
       h.sold_to,
       h.bill_to,
       i.material,
       i.open_quantity,
      ((i.open_quantity) - SUM(a.quantity)) AS 'allocated_quantity',
     SUM(a.quantity) AS 'unallocated_quantity',
       a.req_cat,
       ROUND(SUM(a.quantity)/(i.open_quantity),2) AS 'percent_unallocated',
       SUM(a.quantity) AS 'arun_allocated_qty',
       CASE 
         WHEN i.seasonal_indicator = '18S' 
         THEN 'SS2018' 
         WHEN i.seasonal_indicator = '18F'
         THEN 'FW2018'
         WHEN i.seasonal_indicator = '19S' 
         THEN 'SS2019' 
         WHEN i.seasonal_indicator = '19F' 
         THEN 'FW2019'
         ELSE 'NO SEASON'
       END AS 'seasonal_indicator'

FROM   pdx_sap_user..vw_order_item i 
JOIN  pdx_sap_user..vw_order_header h ON i.sales_order_number = h.sales_order_number 
JOIN   pdx_sap_user..vw_arun_norm_new a ON i.sales_order_number = a.sales_order_number 
AND    i.sales_order_item_number = a.sales_order_item_number


WHERE  i.open_quantity > 0
AND    h.commission_code_id = 'B'
AND    a.stock_type = 'A'

GROUP BY i.plant_code, 
         h.commission_code_id, 
         a.so_conf_del_date,
         h.valid_to_date,
         i.sales_order_number, 
         i.sales_order_item_number,
         h.sold_to,
         h.bill_to,
         i.material,
         i.open_quantity,
         a.req_cat,
         i.unallocated_quantity,
         i.seasonal_indicator

HAVING SUM((a.quantity)/i.open_quantity) > .5) q0 ON m.material = q0.material 
                                                  AND q0.seasonal_indicator = o.season

WHERE    aa.LeadTime = '30'
AND      q0.so_conf_del_date < '01/15/2019'
OR       aa.LeadTime = '45'
AND      q0.so_conf_del_date < '02/01/2019'
OR       aa.LeadTime = '60'
AND      q0.so_conf_del_date < '02/15/2019'
OR       aa.LeadTime = '75'
AND      q0.so_conf_del_date < '03/01/2019'
OR       aa.LeadTime = '90'
AND      q0.so_conf_del_date < '03/15/2019'
OR       aa.LeadTime = '105'
AND      q0.so_conf_del_date < '04/01/2019'
OR       aa.LeadTime = '120'
AND      q0.so_conf_del_date < '04/15/2019'
OR       aa.LeadTime = '135'
AND      q0.so_conf_del_date < '05/01/2019'

AND      q0.sales_order_number NOT IN ( SELECT   q01.sales_order_number

                                        FROM   pdx_sap_user..vw_order_item q01
                                        JOIN   pdx_sap_user..vw_arun_norm_new a1 ON q01.sales_order_number = a1.sales_order_number 
                                        AND    q01.sales_order_item_number = a1.sales_order_item_number
                                        JOIN   asagdwpdx_prod..ArticleNumbers aa1 ON q01.material = aa1.ArticleNumber
                                        JOIN   adi_user_maintained..VW_ONERange_NAM o1 ON q01.material = o1.article 

WHERE  
(
aa1.LeadTime = '30'
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '12/15/2018'
)
OR     
(
aa1.LeadTime = '45' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '01/01/2019'
)
OR     
(
aa1.LeadTime = '60' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '01/15/2019'
)
OR     
(
aa1.LeadTime = '75' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '02/01/2019'
)
OR     
(
aa1.LeadTime = '90' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '02/15/2019'
)
OR     
(
aa1.LeadTime = '90' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '03/01/2019'
)
OR     
(
aa1.LeadTime = '105' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '03/15/2019'
)
AND    q0.sales_order_number = q01.sales_order_number
AND    aa.LeadTime = aa1.LeadTime
AND    q0.so_conf_del_date = a1.so_conf_del_date
AND    o.Country_of_Origin = o1.Country_of_Origin)

GROUP BY kd.business_segment_desc, 
         q0.plant_code,
         q0.req_cat,
         m.[department], 
         q0.commission_code_id,
         aa.LeadTime,
         q0.so_conf_del_date, 
         q0.valid_to_date,
         q0.sales_order_number, 
         q0.sales_order_item_number,
         q0.sold_to, 
         q0.bill_to,
         m.working_number,
         q0.material,
         m.[description],
         q0.open_quantity,
         q0.allocated_quantity,
         q0.unallocated_quantity,
         q0.percent_unallocated,
         aa.ActivationStatus,
         m.ib_lock,
         o.country_of_origin

ORDER BY q0.sales_order_number DESC; 

工作版本:

/* What to do about multiple COO's in ONE RANGE? ...join on order ORDER_ITEM.SEASONAL_INDICATOR? */
/* Use CASE WHEN in subquery to JOIN outerquery on vw_order_item.seasonal_indicator = OneRange_NAM.SEASON */
/* Assumption is being made that 'seasonal indicator' in vw_order_item would align with 'season' and thus COO in OneRange */
/* Added Date/LeadTime Parameters */
/* Use 'Planned Delivery Time'...this includes lead time and delivery time? */
/* Right join to ONE RANGE */
/* ARUN has no fully unallocated quantities - YES IT DOES */
/* 11/12/18 - replaced RDD with CDD */

SELECT kd.business_segment_desc, 
       q0.plant_code,
       q0.req_cat AS 'requirement_category',
       m.[department], 
       q0.commission_code_id,
       aa.LeadTime,
       q0.so_conf_del_date, 
       q0.valid_to_date,
       q0.sales_order_number, 
       q0.sales_order_item_number,
       q0.sold_to, 
       q0.bill_to,
       m.working_number,
       q0.material,
       m.[description],
       q0.open_quantity,
       q0.allocated_quantity,
       q0.unallocated_quantity,
       q0.percent_unallocated,
       aa.ActivationStatus AS 'global status',
       m.ib_lock,
       o.country_of_origin

FROM   pdx_sap_user..vw_mm_material m
JOIN   pdx_sap_user..vw_kd_business_segment kd ON m.business_segment_code = kd.business_segment_code
JOIN   asagdwpdx_prod..ArticleNumbers aa ON m.material = aa.ArticleNumber
JOIN   adi_user_maintained..VW_ONERange_NAM o ON     m.material = o.article 
                                              AND    aa.ArticleNumber = o.Article
JOIN

(SELECT i.plant_code, 
       h.commission_code_id, 
       a.so_conf_del_date,
       h.valid_to_date,
       i.sales_order_number, 
       i.sales_order_item_number,
       h.sold_to,
       h.bill_to,
       i.material,
       i.open_quantity,
      ((i.open_quantity) - SUM(a.quantity)) AS 'allocated_quantity',
     SUM(a.quantity) AS 'unallocated_quantity',
       a.req_cat,
       ROUND(SUM(a.quantity)/(i.open_quantity),2) AS 'percent_unallocated',
       SUM(a.quantity) AS 'arun_allocated_qty',
       CASE 
         WHEN i.seasonal_indicator = '18S' 
         THEN 'SS2018' 
         WHEN i.seasonal_indicator = '18F'
         THEN 'FW2018'
         WHEN i.seasonal_indicator = '19S' 
         THEN 'SS2019' 
         WHEN i.seasonal_indicator = '19F' 
         THEN 'FW2019'
         ELSE 'NO SEASON'
       END AS 'seasonal_indicator'

FROM   pdx_sap_user..vw_order_item i 
JOIN  pdx_sap_user..vw_order_header h ON i.sales_order_number = h.sales_order_number 
JOIN   pdx_sap_user..vw_arun_norm_new a ON i.sales_order_number = a.sales_order_number 
AND    i.sales_order_item_number = a.sales_order_item_number


WHERE  i.open_quantity > 0
AND    h.commission_code_id = 'B'
AND    a.stock_type = 'A'
AND    i.sales_order_number NOT IN (SELECT   q01.sales_order_number

                                        FROM   pdx_sap_user..vw_order_item q01
                                        JOIN   pdx_sap_user..vw_arun_norm_new a1 ON q01.sales_order_number = a1.sales_order_number 
                                        AND    q01.sales_order_item_number = a1.sales_order_item_number
                                        JOIN   asagdwpdx_prod..ArticleNumbers aa1 ON q01.material = aa1.ArticleNumber
                                        JOIN   adi_user_maintained..VW_ONERange_NAM o1 ON q01.material = o1.article 

WHERE  
(
aa1.LeadTime = '30'
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '12/15/2018'
)
OR     
(
aa1.LeadTime = '45' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '01/01/2019'
)
OR     
(
aa1.LeadTime = '60' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '01/15/2019'
)
OR     
(
aa1.LeadTime = '75' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '02/01/2019'
)
OR     
(
aa1.LeadTime = '90' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '02/15/2019'
)
OR     
(
aa1.LeadTime = '90' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '03/01/2019'
)
OR     
(
aa1.LeadTime = '105' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '03/15/2019'
)
AND    q01.sales_order_number = i.sales_order_number)

GROUP BY i.plant_code, 
         h.commission_code_id, 
         a.so_conf_del_date,
         h.valid_to_date,
         i.sales_order_number, 
         i.sales_order_item_number,
         h.sold_to,
         h.bill_to,
         i.material,
         i.open_quantity,
         a.req_cat,
         i.unallocated_quantity,
         i.seasonal_indicator

HAVING SUM((a.quantity)/i.open_quantity) > .5) q0 ON m.material = q0.material 
                                                  AND q0.seasonal_indicator = o.season

WHERE    aa.LeadTime = '30'
AND      q0.so_conf_del_date < '01/15/2019'
OR       aa.LeadTime = '45'
AND      q0.so_conf_del_date < '02/01/2019'
OR       aa.LeadTime = '60'
AND      q0.so_conf_del_date < '02/15/2019'
OR       aa.LeadTime = '75'
AND      q0.so_conf_del_date < '03/01/2019'
OR       aa.LeadTime = '90'
AND      q0.so_conf_del_date < '03/15/2019'
OR       aa.LeadTime = '105'
AND      q0.so_conf_del_date < '04/01/2019'
OR       aa.LeadTime = '120'
AND      q0.so_conf_del_date < '04/15/2019'
OR       aa.LeadTime = '135'
AND      q0.so_conf_del_date < '05/01/2019'

GROUP BY kd.business_segment_desc, 
         q0.plant_code,
         q0.req_cat,
         m.[department], 
         q0.commission_code_id,
         aa.LeadTime,
         q0.so_conf_del_date, 
         q0.valid_to_date,
         q0.sales_order_number, 
         q0.sales_order_item_number,
         q0.sold_to, 
         q0.bill_to,
         m.working_number,
         q0.material,
         m.[description],
         q0.open_quantity,
         q0.allocated_quantity,
         q0.unallocated_quantity,
         q0.percent_unallocated,
         aa.ActivationStatus,
         m.ib_lock,
         o.country_of_origin

ORDER BY q0.sales_order_number DESC

您的手术室短路了。比如看这一段:

SELECT *
FROM Table
WHERE  aa1.LeadTime = '30'
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '12/15/2018'
OR     aa1.LeadTime = '45' 

Returns所有带aa1.LeadTime = '45'的记录,不考虑其他条件。

我猜你是这个意思:

WHERE  
(
aa1.LeadTime = '30'
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '12/15/2018'
)
OR     
(
aa1.LeadTime = '45' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '01/01/2019'
)
OR     
(
aa1.LeadTime = '60' 
AND    o1.country_of_origin IN ('EL SALVADOR','HONDURAS','MEXICO','U S A' ,'GUATEMALA')
AND    a1.so_conf_del_date > '01/15/2019'
)
....
....

这是一个非常不同的表达方式。