SQL where clause conditions

I am stuck with a WHERE clause issue that is giving me headaches…
I want to import in Table B (NewOrder) the data from Table A (t1) but only if the data from Table A do not exist in Table C  (ArchivedOrder) but only if the field [Status] = ‘processing’
This my WHERE clause and until then it seems to be working:
WHERE NOT EXISTS (SELECT * FROM  ArchivedOrder WHERE  t1.OrdNB = ArchivedOrder.PO) AND NOT EXISTS (SELECT * FROM  NewOrder WHERE  t1.OrdNB = NewOrder.PO) AND OrdStatus LIKE 'processing'


The complication comes when I still want to import in Table B from Table A the orders where:
the field [Status] = ‘pending’ and the field [Payment] =’COD’ (both condition must be true)
I don’t know how to add this new condition
pascalmartinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
I guess you want this:
AND ( OrdStatus LIKE 'processing'
    OR ( OrdStatus LIKE 'pending' and Payment = 'COD' )
  )

Open in new window

0
PortletPaulfreelancerCommented:
LIKE requires a wildcard to be effective, otherwise it acts as an equal

either you need to use a wildcard such as this
   OrdStatus LIKE 'processing%' -- i.e. begins with 'processing'

or you need
   OrdStatus = 'processing'

For performance use = in preference to LIKE if you can
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Should be something similar to:
INSERT INTO NewOrder (Col1, Col2, ..., ColN)
SELECT Col1, Col2, ..., ColN
FROM t1 A
WHERE (A.Status = 'Pending' AND A.Payment ='COD')
	OR (A.Status = 'Processing' AND 
			NOT EXISTS (SELECT 1 
			        FROM ArchivedOrder C
			        WHERE A.Id = C.Id))

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
I wasn't sure if the new requirement also included an OrdStatus of 'processing', so I commented it out for.  Either remove it or uncomment it depending on what you want.  Btw, it's a good idea to prefix all columns with the appropriate table alias whenever more than one table is involved in a query.

WHERE
    (t1.[Status] = 'pending' AND t1.[Payment] = 'COD' /* AND t1.OrdStatus = 'processing' */ )
    OR
    (t1.OrdStatus = 'processing' AND
     NOT EXISTS (SELECT * FROM  ArchivedOrder WHERE  t1.OrdNB = ArchivedOrder.PO) AND
     NOT EXISTS (SELECT * FROM  NewOrder WHERE  t1.OrdNB = NewOrder.PO))
0
pascalmartinAuthor Commented:
That Exactly what I wanted
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.