【发布时间】: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