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?
 
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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
 
PortletPaulfreelancerCommented:
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
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.

All Courses

From novice to tech pro — start learning today.