Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Geraldo.

Asked: March 04, 2019 - 12:00 pm UTC

Last updated: March 06, 2019 - 1:17 pm UTC

Version: 18.4.0.0

Viewed 1000+ times

You Asked

Hello, Ask Tom Team.

I have a SQL query but I'm getting unwanted results.

SELECT t1.invoice_sender,t1.einvoice,t3.modified_einvoice,SUBSTR(t3.modified_einvoice,2,2),t2.LOADED_606,t4.APPROVALS    
FROM table1 t1
INNER JOIN table2 t2 on t1.id=t2.id
            LEFT JOIN table3 t3 ON t2.id=t3.id
            INNER JOIN table4 t4 ON (t1.invoice_sender = t4.invoice_sender AND t1.einvoice = t4.einvoice)
WHERE (t2.invoice_type NOT IN (32,41,43) OR SUBSTR(t3.modified_einvoice,2,2) NOT IN (02,32,41,43))
        AND  t2.LOADED_606 = 0 AND t4.APPROVALS = 0 
        AND t4.INV_SENT_DATE < TRUNC(SYSDATE) AND t4.INV_SENT_DATE >=
  (CASE  WHEN TO_NUMBER(TO_CHAR(SYSDATE,'DD')) <= 15 THEN TRUNC(SYSDATE-20,'MM') 
   ELSE TRUNC(SYSDATE,'MM') 
   END)
  AND (t2.invoice_type NOT IN (32,41,43) OR SUBSTR(t3.modified_einvoice,2,2) NOT IN (02,32));


invoice_sender einvoice mod_einvoice SUBSTR(mod_en LOADED_606 APPROVALS
101671866 B340000004003 B3100000199 31 0 0
101671866 B330000004002 B3100000199 31 0 0
101671866 B330000004009 B3200000199 32 0 0
101671866 B340000004010 B3200000199 32 0 0

101671866 B440000004004 0 0
101671866 B310000004000 0 0
101671866 B450000004005 0 0

I do not want row #3 and #4 because of this condition:

AND (t2.invoice_type NOT IN (32,41,43) OR SUBSTR(t3.modified_einvoice,2,2) NOT IN (02,32)

If 02 or 32 is found on modified_einvoice column in position 2 and 3 SUBSTR(t3.modified_einvoice,2,2) then I want that row(s) removed. But it's not working.

Thanks in advanced.

and Chris said...

Without create table + inserts + explanation of what you're trying do, it's kinda hard to figure out what's going on in a SQL query. Please provide these in future.

But...

You're combining conditions with OR.

So this only excludes rows where both the following are true:

t2.invoice_type NOT IN (32,41,43)

SUBSTR(t3.modified_einvoice,2,2) NOT IN (02,32)

So presumably you're getting row #3 & #4 because the invoice_type is NOT 32, 41, or 43.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Review

A reader, March 07, 2019 - 7:03 pm UTC

Thanks for the help.

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.