попробуйте это:
Create Table #MasterMenu(itemCode Bigint,Category Varchar(50))
Insert into #MasterMenu
SElect 10001,'VM1' Union All
SElect 10002,'VM1' Union All
SElect 10003,'VM2' Union All
SElect 10004,'VM3' Union All
SElect 10005,'VM3' Union All
SElect 10006,'HOT DRINKS' Union All
SElect 10007,'HOT DRINKS' Union All
SElect 10008,'COLD DRINKS' Union All
SElect 10009,'COLD DRINKS' Union All
SElect 10066,'DESSERT'
Create Table #Order(SiteId int, BusinessDate Date,ItemName Varchar(50), UnitsSold int,UnitsSale int,ItemCode Bigint , OrderNo Varchar(50))
Insert Into #Order
SELECT 1,'06/08/2018','Apple ',1 ,5 ,10001,'122-1' Union All
SELECT 1,'06/08/2018','Coffee ',1 ,16,10006,'122-1' Union All
SELECT 1,'06/08/2018','Ice Tea',2 ,7 ,10008,'122-1' Union All
SELECT 1,'06/08/2018','Beans ',9 ,18,10004,'122-1' Union All
SELECT 4,'06/08/2018','Donuts ',7 ,17,10066,'122-7' Union All
SELECT 1,'06/08/2018','Bread ',1 ,7 ,10003,'122-4' Union All
SELECT 1,'06/08/2018','Beans ',4 ,8 ,10004,'122-4' Union All
SELECT 2,'06/08/2018','OrangeJuice',2 ,5 ,10009,'122-2' Union All
SELECT 2,'06/08/2018','Coffee ',1 ,6 ,10006,'122-2' Union All
SELECT 3,'06/08/2018','Bread ',3 ,5 ,10003,'122-3' Union All
SELECT 3,'06/08/2018','Beans ',7 ,17,10004,'122-3' Union All
SELECT 3,'06/08/2018','Coffee ',17,17,10006,'122-3' Union All
SELECT 3,'06/08/2018','Ice Tea',7 ,17,10008,'122-5' Union All
SELECT 4,'06/08/2018','OrangeJuice',7 ,17,10009,'122-6'
;with cte
As
(
Select OrderNo,SUM(CASE WHEN ItemCode in (10001,10002,10003) then 1 ELSE 0 END) AS ItemCount
from #Order
Group by OrderNo
)
Select o.* from cte c
INNER JOIN #Order o on c.OrderNo=o.OrderNo
Where c.ItemCount=0
Drop Table #MasterMenu
Drop Table #Order
вывод:
SiteId BusinessDate ItemName UnitsSold UnitsSale ItemCode OrderNo
2 2018-06-08 OrangeJuice 2 5 10009 122-2
2 2018-06-08 Coffee 1 6 10006 122-2
3 2018-06-08 Ice Tea 7 17 10008 122-5
4 2018-06-08 OrangeJuice 7 17 10009 122-6
4 2018-06-08 Donuts 7 17 10066 122-7