2015-10-23 75 views
0

在我的兩列數據中,我想只保留ColumnTwo中具有最高值的ColumnOne的唯一值。SQL:保留C2中具有最高值的C1的唯一值

例如 ColumnOne ColumnTwo

2     6 
3     2 
7     8 
2     7 
3     4 
7     3 

我想結果:

ColumnOne ColumnTwo

2     7 
3     4 
7     8 
+2

這是與'MAX()'的簡單聚集? –

+1

'ColumnOne'從表組中選擇ColumnOne,max(ColumnTwo)? –

回答

0

爲此,您可以用一組由聲明:

select Column1, max(Column2) 
    from your_table 
group by Column1 
0
delete t1 
from myTable t1 
left join (select t2.Column1, max(t2.Column2) maxColumn2 
      from myTable t2 
      group by t2.Column1) tMax 
on t1.Column1 = tMax.Column1 
and t1.Column2 = tMax.maxColumn2 
where tMax.Column1 is null 
0

下面的查詢將幫助你完成你的輸出表中巨大的no。記錄:

create table table1_new as (select * from table1) with no data; 

insert into table1_new 
    select columnone, max(columntwo) over(partition by columnone) from table1 group by columnone; 

驗證數據,然後交換表名:

drop table table1; 
rename table1_new to table1; 
相關問題