SQL NOT EXISTS sub query not functioning as expected










-1















I have an issue where I am using a query to filter my results based on the lead time of an article. The confusing part for me is there is a sub section of that where i need to filter certain articles based on lead time and the country of origin. I am trying to do this using a NOT EXISTSstatement in side of WHERE but the results don't seem to be what I would expect. For example all articles with a country of origin of HONDURAS and a lead time of 90 should not return any results with a so_conf_del_date of after 2/15/19...however in the results I get many results of dates well into march which should be excluded from what I can see. Thought I would get a different set of eyes on it as I really don't use not exists a ton. thanks.



UPDATE: changing to NOT IN from NOT EXISTS and updated the correlation. Note also the subquery performs exactly as it should so not sure why I am getting results back outside of the range



Query UPDATED:



/* 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;


WORKING VERSION:



/* 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









share|improve this question
























  • At a guess.... you need more brackets to define the OR boolean expressions properly. You have an OR (or probably many) which is short circuiting the entire boolean expression.

    – Nick.McDermaid
    Nov 14 '18 at 2:18











  • seems odd because the outer statement doing the same thing on all the other lines works perfect...wondering if it is something to do with the IN statement or maybe how it is connected to the outer query?

    – user3496218
    Nov 14 '18 at 2:33















-1















I have an issue where I am using a query to filter my results based on the lead time of an article. The confusing part for me is there is a sub section of that where i need to filter certain articles based on lead time and the country of origin. I am trying to do this using a NOT EXISTSstatement in side of WHERE but the results don't seem to be what I would expect. For example all articles with a country of origin of HONDURAS and a lead time of 90 should not return any results with a so_conf_del_date of after 2/15/19...however in the results I get many results of dates well into march which should be excluded from what I can see. Thought I would get a different set of eyes on it as I really don't use not exists a ton. thanks.



UPDATE: changing to NOT IN from NOT EXISTS and updated the correlation. Note also the subquery performs exactly as it should so not sure why I am getting results back outside of the range



Query UPDATED:



/* 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;


WORKING VERSION:



/* 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









share|improve this question
























  • At a guess.... you need more brackets to define the OR boolean expressions properly. You have an OR (or probably many) which is short circuiting the entire boolean expression.

    – Nick.McDermaid
    Nov 14 '18 at 2:18











  • seems odd because the outer statement doing the same thing on all the other lines works perfect...wondering if it is something to do with the IN statement or maybe how it is connected to the outer query?

    – user3496218
    Nov 14 '18 at 2:33













-1












-1








-1








I have an issue where I am using a query to filter my results based on the lead time of an article. The confusing part for me is there is a sub section of that where i need to filter certain articles based on lead time and the country of origin. I am trying to do this using a NOT EXISTSstatement in side of WHERE but the results don't seem to be what I would expect. For example all articles with a country of origin of HONDURAS and a lead time of 90 should not return any results with a so_conf_del_date of after 2/15/19...however in the results I get many results of dates well into march which should be excluded from what I can see. Thought I would get a different set of eyes on it as I really don't use not exists a ton. thanks.



UPDATE: changing to NOT IN from NOT EXISTS and updated the correlation. Note also the subquery performs exactly as it should so not sure why I am getting results back outside of the range



Query UPDATED:



/* 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;


WORKING VERSION:



/* 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









share|improve this question
















I have an issue where I am using a query to filter my results based on the lead time of an article. The confusing part for me is there is a sub section of that where i need to filter certain articles based on lead time and the country of origin. I am trying to do this using a NOT EXISTSstatement in side of WHERE but the results don't seem to be what I would expect. For example all articles with a country of origin of HONDURAS and a lead time of 90 should not return any results with a so_conf_del_date of after 2/15/19...however in the results I get many results of dates well into march which should be excluded from what I can see. Thought I would get a different set of eyes on it as I really don't use not exists a ton. thanks.



UPDATE: changing to NOT IN from NOT EXISTS and updated the correlation. Note also the subquery performs exactly as it should so not sure why I am getting results back outside of the range



Query UPDATED:



/* 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;


WORKING VERSION:



/* 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






sql subquery not-exists






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 4:51







user3496218

















asked Nov 14 '18 at 1:49









user3496218user3496218

781313




781313












  • At a guess.... you need more brackets to define the OR boolean expressions properly. You have an OR (or probably many) which is short circuiting the entire boolean expression.

    – Nick.McDermaid
    Nov 14 '18 at 2:18











  • seems odd because the outer statement doing the same thing on all the other lines works perfect...wondering if it is something to do with the IN statement or maybe how it is connected to the outer query?

    – user3496218
    Nov 14 '18 at 2:33

















  • At a guess.... you need more brackets to define the OR boolean expressions properly. You have an OR (or probably many) which is short circuiting the entire boolean expression.

    – Nick.McDermaid
    Nov 14 '18 at 2:18











  • seems odd because the outer statement doing the same thing on all the other lines works perfect...wondering if it is something to do with the IN statement or maybe how it is connected to the outer query?

    – user3496218
    Nov 14 '18 at 2:33
















At a guess.... you need more brackets to define the OR boolean expressions properly. You have an OR (or probably many) which is short circuiting the entire boolean expression.

– Nick.McDermaid
Nov 14 '18 at 2:18





At a guess.... you need more brackets to define the OR boolean expressions properly. You have an OR (or probably many) which is short circuiting the entire boolean expression.

– Nick.McDermaid
Nov 14 '18 at 2:18













seems odd because the outer statement doing the same thing on all the other lines works perfect...wondering if it is something to do with the IN statement or maybe how it is connected to the outer query?

– user3496218
Nov 14 '18 at 2:33





seems odd because the outer statement doing the same thing on all the other lines works perfect...wondering if it is something to do with the IN statement or maybe how it is connected to the outer query?

– user3496218
Nov 14 '18 at 2:33












1 Answer
1






active

oldest

votes


















1














Your OR is short circuiting. For example, look at this piece:



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 all records with aa1.LeadTime = '45' regardless of the other conditions.



I guess you mean this:



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


which is a very different expression.






share|improve this answer























  • ah thanks for that clarification! I assumed for some reason the execution order restarted at each OR, I am going to give that a go!

    – user3496218
    Nov 14 '18 at 3:09











  • still returning results outside of the range - for example lead time of 45 is returning dates ahead of 1/1/2019...will keep digging. thanks.

    – user3496218
    Nov 14 '18 at 3:46











  • your point was definitely correct - to stop getting the results outside of my range i moved the NOT IN to inside the JOIN on q0 and it seemed to solve the issue. Trying to fully understand why. Posted the final code above.

    – user3496218
    Nov 14 '18 at 4:49










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292057%2fsql-not-exists-sub-query-not-functioning-as-expected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your OR is short circuiting. For example, look at this piece:



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 all records with aa1.LeadTime = '45' regardless of the other conditions.



I guess you mean this:



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


which is a very different expression.






share|improve this answer























  • ah thanks for that clarification! I assumed for some reason the execution order restarted at each OR, I am going to give that a go!

    – user3496218
    Nov 14 '18 at 3:09











  • still returning results outside of the range - for example lead time of 45 is returning dates ahead of 1/1/2019...will keep digging. thanks.

    – user3496218
    Nov 14 '18 at 3:46











  • your point was definitely correct - to stop getting the results outside of my range i moved the NOT IN to inside the JOIN on q0 and it seemed to solve the issue. Trying to fully understand why. Posted the final code above.

    – user3496218
    Nov 14 '18 at 4:49















1














Your OR is short circuiting. For example, look at this piece:



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 all records with aa1.LeadTime = '45' regardless of the other conditions.



I guess you mean this:



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


which is a very different expression.






share|improve this answer























  • ah thanks for that clarification! I assumed for some reason the execution order restarted at each OR, I am going to give that a go!

    – user3496218
    Nov 14 '18 at 3:09











  • still returning results outside of the range - for example lead time of 45 is returning dates ahead of 1/1/2019...will keep digging. thanks.

    – user3496218
    Nov 14 '18 at 3:46











  • your point was definitely correct - to stop getting the results outside of my range i moved the NOT IN to inside the JOIN on q0 and it seemed to solve the issue. Trying to fully understand why. Posted the final code above.

    – user3496218
    Nov 14 '18 at 4:49













1












1








1







Your OR is short circuiting. For example, look at this piece:



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 all records with aa1.LeadTime = '45' regardless of the other conditions.



I guess you mean this:



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


which is a very different expression.






share|improve this answer













Your OR is short circuiting. For example, look at this piece:



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 all records with aa1.LeadTime = '45' regardless of the other conditions.



I guess you mean this:



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


which is a very different expression.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 2:45









Nick.McDermaidNick.McDermaid

11.9k32555




11.9k32555












  • ah thanks for that clarification! I assumed for some reason the execution order restarted at each OR, I am going to give that a go!

    – user3496218
    Nov 14 '18 at 3:09











  • still returning results outside of the range - for example lead time of 45 is returning dates ahead of 1/1/2019...will keep digging. thanks.

    – user3496218
    Nov 14 '18 at 3:46











  • your point was definitely correct - to stop getting the results outside of my range i moved the NOT IN to inside the JOIN on q0 and it seemed to solve the issue. Trying to fully understand why. Posted the final code above.

    – user3496218
    Nov 14 '18 at 4:49

















  • ah thanks for that clarification! I assumed for some reason the execution order restarted at each OR, I am going to give that a go!

    – user3496218
    Nov 14 '18 at 3:09











  • still returning results outside of the range - for example lead time of 45 is returning dates ahead of 1/1/2019...will keep digging. thanks.

    – user3496218
    Nov 14 '18 at 3:46











  • your point was definitely correct - to stop getting the results outside of my range i moved the NOT IN to inside the JOIN on q0 and it seemed to solve the issue. Trying to fully understand why. Posted the final code above.

    – user3496218
    Nov 14 '18 at 4:49
















ah thanks for that clarification! I assumed for some reason the execution order restarted at each OR, I am going to give that a go!

– user3496218
Nov 14 '18 at 3:09





ah thanks for that clarification! I assumed for some reason the execution order restarted at each OR, I am going to give that a go!

– user3496218
Nov 14 '18 at 3:09













still returning results outside of the range - for example lead time of 45 is returning dates ahead of 1/1/2019...will keep digging. thanks.

– user3496218
Nov 14 '18 at 3:46





still returning results outside of the range - for example lead time of 45 is returning dates ahead of 1/1/2019...will keep digging. thanks.

– user3496218
Nov 14 '18 at 3:46













your point was definitely correct - to stop getting the results outside of my range i moved the NOT IN to inside the JOIN on q0 and it seemed to solve the issue. Trying to fully understand why. Posted the final code above.

– user3496218
Nov 14 '18 at 4:49





your point was definitely correct - to stop getting the results outside of my range i moved the NOT IN to inside the JOIN on q0 and it seemed to solve the issue. Trying to fully understand why. Posted the final code above.

– user3496218
Nov 14 '18 at 4:49



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292057%2fsql-not-exists-sub-query-not-functioning-as-expected%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20