如何找到SQL Server中某些条件不存在的记录?

时间:2017-03-26 13:47:14

标签: sql sql-server stored-procedures

我有两张桌子。

具有30k记录的ItemRelation表

ID     ChildID1     ChildID2     ChildID3
------------------------------------------
9      null         null          null
49     43           50                       //43 in childid1, don't want this record too
111    112          113           null
65     68           null          null
222    221          223           224
79     null         null          null
5773   5834         5838          null

F_ItemDailySalesParent拥有数百万条记录

ItemID              StoreId
-----------------
9         1001   //ItemID 9,41,5773 belongs to 1001 StoreID
41        1001   
43        1400   //ItemID 43,45,65,5834 belongs to 1400 StoreID
45        1400
65        1400
68        2000   //ItemID 68,79 belongs to 2000 StoreID
79        2000
5773      1001
5834      1400
5838      2000 

我想显示ItemRelation表中的记录ID,其中来自F_ItemDailySalesParent的ItemID不存在于ItemRelation中

ItemID    StoreID
-----------------
49        1001
111       1001
65        1001
222       1001
79        1001

9         1400
111       1400
222       1400
79        1400

9         2000
49        2000
111       2000
222       2000
5773      2000

我尝试了以下查询。但这将没有StoreID。但不知道上述结果

select ID from HQMatajer.dbo.ItemRelation ir
where not exists(
    select ID,StoreID
    from [HQWebMatajer].[dbo].[F_ItemDailySalesParent] Fid
    where fid.ItemID=ir.ID 
        or fid.ItemID = ir.ChildID1 
        or Fid.ItemID=ir.ChildID2 
        or Fid.ItemID=ir.ChildID3 
        and time between '2017-01-01 00:00:00.000' and '2017-02-28 00:00:00.000'
     group by ItemID,StoreID
  )

更新

  

Hqmatajer.dbo.Store列名为storeCode = F_ItemDailySalesParent.Storeid

3 个答案:

答案 0 :(得分:1)

使用StoreId

时,请检查not exists()是否匹配
select ID 
from HQMatajer.dbo.ItemRelation ir
cross join (select distinct storeCode from Hqmatajer.dbo.Store) s
where not exists(
    select 1
    from [HQWebMatajer].[dbo].[F_ItemDailySalesParent] Fid
    where fid.StoreId = s.StoreCode
     and [time] between '2017-01-01 00:00:00.000' and '2017-02-28 00:00:00.000'
     and ( fid.ItemID=ir.ID 
        or fid.ItemID=ir.ChildID1 
        or Fid.ItemID=ir.ChildID2 
        or Fid.ItemID=ir.ChildID3 
        )
  )

答案 1 :(得分:0)

如果我理解正确,您希望从所有商店和商品的列表开始,然后筛选出存在的商品和商品。

select i.id, s.storeId
from (select distinct id from HQMatajer.dbo.ItemRelation ir) i cross join
     stores s  -- assume this exists
where not exists (select 1
                  from [HQWebMatajer].[dbo].[F_ItemDailySalesParent] idsp
                  where idsp.ItemID = i.ID and idsp.storeId = s.storeId
                 ) and
      not exists (select 1
                  from [HQWebMatajer].[dbo].[F_ItemDailySalesParent] idsp
                  where idsp.ItemID = i.childID1 and idsp.storeId = s.storeId
                 ) and
      not exists (select 1
                  from [HQWebMatajer].[dbo].[F_ItemDailySalesParent] idsp
                  where idsp.ItemID = i.childID2 and idsp.storeId = s.storeId
                 ) and
      not exists (select 1
                  from [HQWebMatajer].[dbo].[F_ItemDailySalesParent] idsp
                  where idsp.ItemID = i.childID3 and idsp.storeId = s.storeId
                 );

我没有包含time条件。它不在您的样本数据中,因此不清楚其适合的位置。

答案 2 :(得分:0)

首先获取ItemIds的唯一列表和StoreID的唯一列表,然后您可以看到左连接缺少哪些,以及交叉引用表id为null。我会用通用术语来做,所以你明白了:

select s.StoreId, i.ItemId
from Stores s
cross apply Items i
left join ItemRelation ir
on s.StoreId = ir.StoreId
and i.ItemId = ir.ItemId
where ir.Id is null