SQL Query

hi
i have rows in my db that present customer orders
there can be a lot of orders for each customer
i need to pull out only customer that have only close orders
for example :
CustName  | CustNum | OrderNum | IsClosed |
xxx                        1                100                  1
xxx                        1                101                  0
yyy                        2                 105                 1
yyy                        2                 110                 1
ttt                         3                 333                  0

after i run the query i will get CustNum
2
picos all the order are close for this customer
that is the query that i need

thanks ...
Tech_MenAsked:
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.

chaauCommented:
You can use the HAVING clause for this
SELECT CustNum
FROM orders
GROUP BY CustNum
HAVING MAX(IsClosed) = 1 and MIN(IsClosed) = 1

Open in new window

0
Tech_MenAuthor Commented:
thanks for your quick answer
but i am getting this msg :
Operand data type bit is invalid for max operator.
IsClosed is a bit column
0
chaauCommented:
Do this then:
SELECT CustNum
FROM orders
GROUP BY CustNum
HAVING MAX(CASE WHEN IsClosed=1 THEN 1 ELSE 0 END) = 1 
     and MIN(CASE WHEN IsClosed=1 THEN 1 ELSE 0 END) = 1

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
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Harshad MCommented:
Following would work (Not tested)

SELECT DISTINCT CustNum
FROM orders
where CustNum NOT IN
(SELECT CustNum FROM ORDERS WHERE IsClosed = 0)
0
Mark ElySenior Coldfusion DeveloperCommented:
SELECT CustName, CustNum, IsClosed
FROM NameOfYourTable
WHERE IsClosed = 1
GROUP BY CustNum, CustName, IsClosed

Open in new window

0
PortletPaulEE Topic AdvisorCommented:
SELECT CustNum
FROM orders
GROUP BY CustNum
HAVING MIN(CAST(IsClosed as smallint)) = 1

Use CAST() on the bit column as indicated. Also you only need to test for the minimum as the column can only be zero or one, if there are any zeros that will be the minimum, and if that is true than there are some open orders for that customer which you want to ignore.

&:
a where clause "WHERE IsClosed = 1" would not satisfy the requirement to locate customers with only closed orders.
0
Scott PletcherSenior DBACommented:
SELECT o.CustName, o.CustNum, o.OrderNum, o.IsClosed --,o....
FROM orders o
INNER JOIN (
    SELECT CustNum
    FROM orders
    GROUP BY CustNum
    HAVING MIN(CAST(IsClosed AS tinyint)) = 1    
) AS o_closed_only ON
    o_closed_only.CustNum = o.CustNum
0
Tech_MenAuthor Commented:
thanks
but only 2 of the answers are working
the best work better picos she didn't include null values also

thanks all
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.