【问题标题】:How to find the record which is not exists with some criteria in SQL Server?如何在 SQL Server 中查找某些条件下不存在的记录?
【发布时间】:2017-03-27 00:24:20
【问题描述】:

我有两张桌子。

具有 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.StorestoreCode = F_ItemDailySalesParent.Storeid的列名

【问题讨论】:

  • 您在问题中的查询不正确。 . .至少,它不太可能做任何有用的事情。
  • @PhamX.Bach 是不同的。我的表中有重复的值。所以我应该做分组
  • @GordonLinoff 是的,这不是正确的查询,请注意我在查询上方的观点。如果F_ItemDailySalesParent 表中没有StoreID 字段。这将带来不存在的记录。

标签: sql sql-server stored-procedures


【解决方案1】:

包括在使用not exists() 时检查StoreId 是否匹配

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 
        )
  )

【讨论】:

  • ItemRelation 表没有StoreID 字段
  • @mohamedfaiz 更新为包含一个交叉连接,只要StoreId 是主键。
  • @mohamedFaiz StoreId 来自哪里?除了F_ItemDailySalesParent 之外还有其他地方吗?您可以从F_ItemDailySalesParent 中获取StoreId 的不同列表,但这比使用已经唯一的列表要慢。
  • @mohamedfaiz StoreCodeHqmatajer.dbo.Store 中是唯一的,对吗?
  • 谢谢兄弟。请告诉我如何提高 Sql 技能。
【解决方案2】:

如果我理解正确,您希望从所有商店和商品的列表开始,然后过滤掉存在的那些。

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 条件。它不在您的示例数据中,因此不清楚它适合的位置。

【讨论】:

    【解决方案3】:

    首先获取唯一的 ItemId 列表和唯一的 StoreID 列表,然后您可以通过左连接和交叉引用表 id 为空来查看缺少哪些。我会用通用术语来做,这样你就明白了:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-09
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多