I have productionorders in one table "ProdTable": 'WO15001, WO15002,..'Each order can go through a series of different operations assigned to workcenters. These operations are defined in the table "ProdRoute"ProdID WorkCenter--------------------WO15001 1WO15001 2WO15001 3WO15002 2WO15002 4WO15003 4WO15004 ....
I need a query that returns only those WorkOrders that go ONLY through WorkCenter '4' and do NOT have any other Workcenters involved in their route.In this example only WO15003 would be returned!I came up with a solution which is rather lenghty and complicated (join/group/count/re-join) and was asking myself if there wasn't a smoother way to do it, taking maybe advantage of the cool features in SQL2012.SELECT a.ProdID, a.CNTFROM (SELECT ProdRoute.ProdID, COUNT(ProdRoute.WorkCenter) as CNT FROM ProdTable inner join ProdRoute on ProdTable.ProdID = ProdRoute.ProdID GROUP BY ProdRoute.ProdID)a inner join ProdRoute on a.ProdID = ProdRoute.ProdID and a.CNT = 1 and ProdRoute.WorkCenter = '4'
Any ideas?Martin