考虑一个包含以下模式的表:
id, location, starred
有许多记录具有相同的位置:
id | location | starred
-----------------------
1 rome yes
2 rome no
3 rome no
4 milan yes
5 milan no
6 bozen no
我希望每个位置最多只有一条记录。考虑到主演唱片和非主演唱片之间的选择,我希望主演。 那么sql会生成这个表:
id | location | starred
-----------------------
1 rome yes
4 milan yes
6 bozen no
我怀疑这可以通过一些虚拟表或ªviews来完成。
DELETE FROM table
GROUP BY location,
答案 0 :(得分:3)
使用分析功能删除重复项。下面的代码生成基于row_number的位置,并按星号desc排序(所以肯定是第一个)
delete from mytable2 where id in (
select id from
( select id, location,starred,row_number() over ( partition by location order by location, starred desc) row_num
from mytable2
) where row_num >1
)
答案 1 :(得分:1)
如果[started]只能是yes或no,那么这应该有效:
create table data
(
id int identity(1,1),
location varchar(50),
[started] varchar(3)
)
insert into data select 'Rome', 'Yes'
insert into data select 'Rome', 'No'
insert into data select 'Rome', 'No'
insert into data select 'Milan', 'Yes'
insert into data select 'Milan', 'No'
insert into data select 'Bozen', 'No'
WITH locationsRanked (id, location, [started], rank)
AS
(
select min(Id), location, [started],
RANK() OVER (PARTITION BY location ORDER BY location, [started] DESC) AS Rank
from data
group by location, [started]
)
select * from locationsRanked where Rank = 1
order by id
答案 2 :(得分:0)
如果你只是想提取这样的数据应该有用:
select
[table].*
from
[table]
inner join (select
MIN(id) as id,
location
from
[table]
group by location) as data
on [table].id = data.id
显然,您也可以使用此结果(或类似查询)来确定要删除的ID列表。
答案 3 :(得分:-1)