Oracle SQL 如何通过特定列消除重复行

Oracle SQL how to eliminate duplicated row by specific column

例如,我在一个 table

中有如下值
 Table A                          
 ----------------                 
 item_name price departure        
 ----------------                 
 shoe      10    150
 shoe      10    150              
 socks     2     100              
 socks     2     110
 shirt     5     170
 shirt     5     170            
 gloves    1     210  
 gloves    1     210
 gloves    1     210    

我想 select 所有具有唯一性 item_name 的行,所以简单的方法是使用 UNION

select item_name, price, departure from table A
UNION 
select item_name, price, departure from table A

但是就像你看到的袜子有不同的偏离,我的结果是错误的,因为我得到了结果

 Table A                          
 ----------------                 
 item_name price departure        
 ----------------                 
 shoe      10    150             
 socks     2     100              
 socks     2     110
 shirt     5     170          
 gloves    1     210  

你能帮我吗,我正在寻找没有左连接的简单方法,因为 table A 包含大量数据,我想优化

我想得到如下结果,其中袜子的最小部分相同(item_name 和价格)

Table A                          
 ----------------                 
 item_name price departure        
 ----------------                 
 shoe      10    150             
 socks     2     100              
 shirt     5     170          
 gloves    1     210  

感谢帮助

我认为你只需要聚合:

select item_name, price, min(departure) departure
from mytable
group by item_name, price

Demo on DB Fiddle:

ITEM_NAME | PRICE | DEPARTURE
:-------- | ----: | --------:
shoe      |    10 |       150
socks     |     2 |       100
shirt     |     5 |       170
gloves    |     1 |       210

使用 row_number() 是从 table 中选择特定行的有效方法。在 over() 子句中使用 partition byorder by 来控制行号何时重置为 1:

select
    *
from (
    select
         item_name
       , price
       , departure
       , row_number() over(partition by  item_name order by price, departure) as rn
    from mytable a  
    ) d
where rn = 1
ITEM_NAME | PRICE | DEPARTURE | RN
:-------- | ----: | --------: | -:
gloves    |     1 |       210 |  1
shirt     |     5 |       170 |  1
shoe      |    10 |       150 |  1
socks     |     2 |       100 |  1

db<>fiddle here