2012-06-27 43 views
1

考慮一個表具有以下模式:SQL使用一些常用字段值刪除重複記錄?

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

所以你要在模式中只選擇不同的城市,如果有重複,你想要一個'starred'= yes? – vandershraaf

+0

是的,也許創建該選擇查詢的物化視圖。 – simpatico

回答

1

如果[開始]只能是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 
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列表。

+0

您忽略了已加星標的約束 – simpatico

3

使用分析函數刪除重複項。下面的代碼生成基於ROW_NUMBER的位置,並通過出演降序排序(所以是至上)

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

這將是特定於MS SQL Server的。理想的解決方案,但是。 –

+0

我正在使用mysql4 – simpatico