根据条件插入表的每一列中

问题描述 投票:0回答:1

我有一张这样的产品表:

“屏幕截图”

我想删除此表中的重复行并在其他表中使用ID,因此我使用了一个临时表来仅添加要删除的ID和要保留的ID:

-- create tmp table
create table #tmp (ProductId_ToKeep int, ProductId_ToDelete int);

-- collect the Products that have a lower id with the same name in the temp table
insert into #tmp (ProductId_ToKeep)
select [ProductId]
from dbo.[Product] t1
where exists
(
    select 1
    from dbo.[Product] t2
    where t2.name = t1.name
      and t2.[ProductId] > t1.[ProductId]
);
-- collect the Products that have a higher id with the same name in the temp table
insert into #tmp (ProductId_ToDelete)
select [ProductId]
from dbo.[Product] t1
where exists
(
    select 1
    from dbo.[Product] t2
    where t2.name = t1.name
      and t2.[ProductId] < t1.[ProductId]
);
select * from #tmp

获得临时表中的内容后,得到以下结果:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS90VGk4ZS5wbmcifQ==” alt =“结果。”>

我问是否有什么可以帮助我根据需要将ID放入每一列中。

sql sql-server tsql duplicates temp-tables
1个回答
0
投票

如果我正确地跟随了您,则可以使用窗口函数在单个查询中提供转码表,如下所示:

insert into #tmp (ProductId_ToKeep, ProductId_ToDelete)
select *
from (
    select 
        ProductId ProductId_ToDelete, 
        min(ProductId) over(partition by name) ProductId_ToKeep
    from dbo.[Product]
) t
where ProductId_ToDelete != ProductId_ToKeep

内部查询为给定的ProductId提取最小的name;外部查询过滤器应删除记录(即ProductId不是相同名称的最小ProductId)。

© www.soinside.com 2019 - 2024. All rights reserved.