;with allCombinations as (select I.*, B.* from Items I, Branches B) select AC.* from allCombinations AC where not exist (select 1 from BranchItems BI where BI.ItemId = AC.ItemID and BI.BranchId = AC.BranchID)>>>Hi,
>>>use test >>> >>>create table Items >>>(ItemId int, >>>ItemCode char(1)) >>> >>>insert into Items values (1, 'A') >>>,(2, 'B') >>>,(3, 'Z') >>> >>>create table Branches >>>(BranchID int, >>>BranchCode char(1)) >>> >>>insert into Branches values (1, 'C') >>>,(2, 'D') >>> >>>create table BranchItems >>>(BranchItemId int >>>,ItemID int >>>,BranchID int >>>,QtyOnHand int) >>> >>>insert into BranchItems values (1, 1, 1, 10) >>>,(2,1,2,15) >>>,(3,2,1,20) >>> >>>select ItemCode, BranchCode, QtyOnHand >>>from branchitems >>>inner join Items on BranchItems.itemID = Items.ItemId >>>inner join Branches on BranchItems.BranchID = Branches.BranchID >>> >>>/* >>>ItemCode BranchCode QtyOnHand >>>A C 10 >>>A D 15 >>>B C 20 >>>*/ >>> >>>drop table Items >>>drop table Branches >>>drop table BranchItems>>>
select I.* >from Items I >where not exists (select * > from BranchItems BI > where BI.ItemID = I.ItemID)>
select I.ItemID, B.BranchID, I.ItemCode, B.BranchCode >from Items I, Branches B >where not exists (select ItemCode, BranchCode, QtyOnHand, BI.ItemID, BI.BranchID > from branchitems BI > inner join Branches on BI.BranchID = Branches.BranchID > inner join Items ON BI.ItemId = Items.ItemID > where BI.ItemID = I.ItemID > and BI.BranchID = B.BranchID)