如何从预算中仅选择前1个补丁以删除重复记录
update r
set Comp = t.Comp
from [QlikDataWarehouse].[dbo].[Vente]r with (NOLOCK) inner join
[QlikDataWarehouse].[dbo].[Budget] t
on t.[Code Site] = r.[Code Site] and
t.[Code Rayon] = substring(r.[Code Structure],1,4) and
t.[Date Time] = convert(date,r.[Date Time])
where r.[Date Time] >= '2015-01-01 00:0:00.000';
答案 0 :(得分:0)
尝试这样的事情:
with topComp as (
select t.[Code Site], t.[Code Rayon], t.[Date Time], max(t.[Comp])
from [QlikDataWarehouse].[dbo].[Budget] t
group by t.[Code Site], t.[Code Rayon], t.[Date Time]
);
update r
set Comp = t.Comp
from [QlikDataWarehouse].[dbo].[Vente]r with (NOLOCK) inner join
topComp t
on t.[Code Site] = r.[Code Site] and
t.[Code Rayon] = substring(r.[Code Structure],1,4) and
t.[Date Time] = convert(date,r.[Date Time])
where r.[Date Time] >= '2015-01-01 00:0:00.000';
答案 1 :(得分:0)
如果我理解正确,您有budget
表的多个匹配项,而您只需要一个匹配项。如果是,请使用cross apply
代替join
。例如,要获取最新记录:
update r
set Comp = t.Comp
from [QlikDataWarehouse].[dbo].[Vente]r with (NOLOCK) cross apply
(select top 1 t.*
from [QlikDataWarehouse].[dbo].[Budget] t
where t.[Code Site] = r.[Code Site] and
t.[Code Rayon] = substring(r.[Code Structure],1,4) and
t.[Date Time] = convert(date,r.[Date Time])
order by t.[Date Time] desc
) t
where r.[Date Time] >= '2015-01-01 00:0:00.000';
答案 2 :(得分:0)
我认为您可以使用与Skipping records in subtable中相同的方法
如果没有Id
列,则可以执行大于[Date Time]