Using OR in SQL clause

Hi,

I have a sql query looking like this:
SELECT o.orderID, o.invoiceID, o.customerID, c.firstname + ' ' + c.lastname AS Name, o.invoiceDate, o.PayDate, o.ocr, paymentStatusID, orderStatusID, ISNULL(printed,0) AS printed, currencyID, paymentTypeID  
FROM orders AS o INNER JOIN Customer AS c ON o.customerID = c.CustomerID  
WHERE (o.invoiceID > '0') AND (o.orgID = 11) AND orderSaved = 1 AND o.orderStatusID > 0 AND (old IS NULL OR Old = 0) 
AND orderStatusID = 5 
OR PaymentTypeID  = 12
ORDER BY CONVERT (BIGINT, o.invoiceID) DESC

Open in new window

The problem is now that when I use the OR keyword it chooses between this clause:
WHERE (o.invoiceID > '0') AND (o.orgID = 11) AND orderSaved = 1 AND o.orderStatusID > 0 AND (old IS NULL OR Old = 0) 
AND orderStatusID = 5 

Open in new window

and this:
OR PaymentTypeID  = 12

Open in new window


I can solve this by doing like this and this reflects the result I really want.
SELECT o.orderID, o.invoiceID, o.customerID, c.firstname + ' ' + c.lastname AS Name, o.invoiceDate, o.PayDate, o.ocr, paymentStatusID, orderStatusID, ISNULL(printed,0) AS printed, currencyID, paymentTypeID  
FROM orders AS o INNER JOIN Customer AS c ON o.customerID = c.CustomerID  
WHERE (o.invoiceID > '0') AND (o.orgID = 11) AND orderSaved = 1 AND o.orderStatusID > 0 AND (old IS NULL OR Old = 0) 
AND orderStatusID = 5 
OR PaymentTypeID  = 12
AND (o.invoiceID > '0') AND (o.orgID = 11) AND orderSaved = 1 AND o.orderStatusID > 0 AND (old IS NULL OR Old = 0) 
AND orderStatusID = 5 
ORDER BY CONVERT (BIGINT, o.invoiceID) DESC

Open in new window


I wonder however if there is a smarter way to use the OR keyword so I don't have to repeat everything in the WHERE clause every time I use it?!

Thanks for help!

Peter
Peter NordbergIT ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Vitor MontalvãoMSSQL Senior EngineerCommented:
It's all about the precedence of the OR operator. You need to use parentheses:
SELECT o.orderID, o.invoiceID, o.customerID, c.firstname + ' ' + c.lastname AS Name, o.invoiceDate, o.PayDate, o.ocr, paymentStatusID, orderStatusID, ISNULL(printed,0) AS printed, currencyID, paymentTypeID  
FROM orders AS o INNER JOIN Customer AS c ON o.customerID = c.CustomerID  
WHERE (o.invoiceID > '0') AND (o.orgID = 11) AND orderSaved = 1 AND o.orderStatusID > 0 AND (old IS NULL OR Old = 0) 
AND (orderStatusID = 5 OR PaymentTypeID  = 12)
ORDER BY CONVERT (BIGINT, o.invoiceID) DESC

Open in new window

0
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
You need to use brackets.

So you want lines 3 to 5 of your first code to read:


WHERE (o.invoiceID ...etc
AND (orderStatusID =5
OR PaymentTypeID = 12)

Note the additional brackets in lines 4 and 5.
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
Peter NordbergIT ManagerAuthor Commented:
Hi,

I tried like this:
SELECT o.orderID, o.invoiceID, o.customerID, c.firstname + ' ' + c.lastname AS Name, o.invoiceDate, o.PayDate, o.ocr, paymentStatusID, orderStatusID, ISNULL(printed,0) AS printed, currencyID, paymentTypeID  
FROM orders AS o INNER JOIN Customer AS c ON o.customerID = c.CustomerID  
WHERE (o.invoiceID > '0') AND (o.orgID = 11) AND orderSaved = 1 AND o.orderStatusID > 0 AND (old IS NULL OR Old = 0) 
AND (orderStatusID = 5  OR PaymentTypeID = 12)
ORDER BY CONVERT (BIGINT, o.invoiceID) DESC 

Open in new window


But then I get this result (two of the instances are doubled)

SQL result
Peter
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
They aren't duplicated. They has different orderID, orderStatusID and paymentTypeID.
If you want only one of them then you need to say which one should be returned.
0
Peter NordbergIT ManagerAuthor Commented:
You're right. I was to fast in my judgement.

Peter
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
Query Syntax

From novice to tech pro — start learning today.