简体   繁体   中英

SQL query to return value based on conditions are not working

My query should not return 1 when both of the value of (srf1.shipment_refnum_qual_gid = 'DUCAB.EXCISE_INVOICE_ATTACHED' and srf2.shipment_refnum_qual_gid = 'DUCAB.BOL_ATTACHED') is equal to Y. But one of the Refnum can have one value as Y.

Currently my query is working when both of the values are N or one of the values are N but whenever my values of refnum are Y then my query is not working.

I am new to SQL - please help me to understand.

I have tried using IN, but it is not working.

select 1 
from shipment sh, shipment_refnum srf1, shipment_refnum srf2, shipment_remark srk
where 
    sh.shipment_gid = srf1.shipment_gid
    and sh.shipment_gid = srf2.shipment_gid 
    and srf1.shipment_gid = srk.shipment_gid 
    and srf2.shipment_gid = srk.shipment_gid
    and srf1.shipment_refnum_qual_gid = 'DUCAB.EXCISE_INVOICE_ATTACHED'
    and srf2.shipment_refnum_qual_gid = 'DUCAB.BOL_ATTACHED'
    and srf1.shipment_refnum_value in ('Y','N')
    and srf2.shipment_refnum_value in ('Y','N')
    and srk.remark_qual_gid = 'DUCAB.REASON_FOR_REJECTION'
    and srk.remark_text not in ('NO_VALUE')
    and sh.shipment_gid = 'DUCAB.20110'

So, my desired outcome is if shipment_refnum_value of 'DUCAB.EXCISE_INVOICE_ATTACHED' is Y and shipment_refnum_value of 'DUCAB.BOL_ATTACHED' is Y then my query should not return 1.

First, your query is just returning 1, no other context. Typically a select query will return multiple records showing different columns from the different tables. So, I am altering your query to show the shipments and ref nums that appear to qualify what you describe you are looking for... Only the srf1 OR srf2 can = "Y", not both = "Y".

Second, I am adjusting your query to using JOIN syntax instead of via WHERE declaration.

Third, via transitive association, if A = B and B = C, then A = C.
So from

sh.Shipment_gid = srf1.Shipment_gid and srf1.shipment_gid = srk.shipment_gid 

then

sh.Shipment_gid = srk.shipment_gid 




select 
      sh.shipment_gid,
      1 
   from 
      shipment sh
         JOIN shipment_refnum srf1
            ON sh.shipment_gid = srf1.shipment_gid
           and srf1.shipment_refnum_qual_gid = 'DUCAB.EXCISE_INVOICE_ATTACHED'
           and srf1.shipment_refnum_value in ('Y','N')
         JOIN shipment_refnum srf2
            ON sh.shipment_gid = srf2.shipment_gid
           and srf2.shipment_refnum_qual_gid = 'DUCAB.BOL_ATTACHED'
           and srf2.shipment_refnum_value in ('Y','N')
         JOIN shipment_remark srk
            ON sh.shipment_gid = srk.shipment_gid 
           and srk.remark_qual_gid = 'DUCAB.REASON_FOR_REJECTION'
           and srk.remark_text not in ('NO_VALUE')
   where 
          sh.shipment_gid = 'DUCAB.20110'
      -- THIS Clause makes sure only ONE of them is a Y 
      -- hence not equal to each other.
      AND srf1.shipment_refnum_value <> srf2.shipment_refnum_value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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