Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In following table with rows specified, I need to get distinct column data e.g.

table name: Stock
C#
ProductID	SerialNo	InvoiceNo	Status
000001	          0001		   1           PURCHASE
000001	          0002		   1           PURCHASE
000002	          0001		   2           PURCHASE
000002	          0002		   2           PURCHASE
000002	          0003		   2           PURCHASE
000002	          0004		   3           ORDER
000003	          0001		   3           ORDER
000004	          0001		   3           ORDER
000005	          0001	           4	       PAINTING
000005	          0002		   4           ORDER
000006	          0001	           4	       ORDER
000007	          0001		   5           ORDER
000008	          0001		   5           ORDER
000009	          0001	           6	       PAINTING
000009	          0002		   6           ORDER


in this scenario I want result should be return only InvoiceNo which having All productid status as 'Order'
here output must be 3,5

I want this in single query.
Posted
Updated 17-Jul-14 23:35pm
v2
Comments
Thanks7872 18-Jul-14 5:36am    
Why not Invoice no. 4 and 6? They are also having status= order?
nilesh sawardekar 18-Jul-14 5:37am    
4 AND 6 also has status as 'PAINTING'
nilesh sawardekar 18-Jul-14 5:39am    
Hows you update it.. :)

Hi,

Check this one.


SQL
SELECT InvoiceNo from Stock where Status='ORDER' and InvoiceNo not in (select InvoiceNo from Stock where Status <> 'ORDER' )


Hope this will help you.
 
Share this answer
 
Comments
nilesh sawardekar 19-Jul-14 1:24am    
Even its looks much simple and logical..its not working..
Try this

SQL
select *
from   Stock S
       INNER JOIN 
       (
          select invoiceNo, count(distinct status)
          from  Stock
          where count(distinct status) = 1
       ) T on S.InvoiceNo = T.InvoiceNo
where  S.Status = 'Order'
 
Share this answer
 
Comments
nilesh sawardekar 19-Jul-14 1:24am    
Join not supporting in access database..
SELECT * FROM Stock WHERE Status = 'ORDER' AND ProductID NOT IN(SELECT ProductID FROM Stock WHERE Status <> 'ORDER')
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900