我遇到以下SQL查询的where语句时遇到问题:
select distinct
[columns]
from ifsapp.customer_order_inv_head coih
left outer join ifsapp.identity_invoice_info iii
on coih.company = iii.company
and coih.identity = iii.identity
and iii.party_type_db = 'CUSTOMER'
left outer join ifsapp.customer_info_address cia
on coih.identity = cia.customer_id
and cia.address_id = '1'
left outer join ifsapp.customer_info_comm_method cicm
on coih.identity = cicm.customer_id
and cicm.address_id = '1'
where coih.invoice_date between to_date('&date_from', 'dd/mm/yyyy') and to_date('&date_to', 'dd/mm/yyyy')
and iii.group_id not in ('1', '2', '3', '4', '5', '6', '7', '8', '9')
and iii.pay_term_id <> 'SO'
and ifsapp.fb_dev_utils_api.is_einvoice_customer(coih.identity) = 'FALSE'
and ifsapp.fb_dev_utils_api.is_edi_customer(coih.identity) = 'FALSE'
and coih.net_amount >= ('&Value')
and upper(cicm.name) not like upper(nvl('&Optout', 'blank'))
and upper(iii.group_id) like upper(nvl('&Cust_Group', '%'))
and upper(coih.company) like upper(nvl('&Company', '%'))
以上工作正常(如果有点慢),但我也想仅在iii.group_id = '10'
时排除coih.contract not like 'S%'
,但我无法正确理解语法。我尝试过以下方法:
or (coih.contract not like 'S%' and iii.group_id = '10')
非常感谢任何帮助。
答案 0 :(得分:0)
而不是:
OR (coih.contract NOT LIKE 'S%' AND iii.group_id = '10')
试试这个:
AND NOT (coih.contract LIKE 'S%' AND iii.group_id = '10')
通过这种方式,它将查看group_id是否等于10且合同不是以'S'开头。
答案 1 :(得分:0)
你有点走上正轨。但是where
语句是关于包含的内容,而不是排除。您想要包含以下记录:
and (coih.contract like 'S%' or iii.group_id <> '10')
您可能会发现此表单更容易理解:
and not (coih.contract not like 'S%' and iii.group_id = '10')
顺便说一下,这假设两个变量不采用NULL值。