选择带有自定义金额Oracle的重复行

时间:2018-08-03 06:55:37

标签: php sql oracle greatest-n-per-group

在[PERFORMANCE]表中,某些行的PERFORMANCE_INDICATOR列的值相同。

ID | PERFORMANCE_INDICATOR                 | REALISASI |
---------------------------------------------------------
 1 | Compliance of Inventory Data Consumer | 90,91     |
 7 | Compliance of Inventory Data Consumer | 92,22     |
13 | Compliance of Inventory Data Consumer | 93,31     |
 9 | Migrasi ODF to FTM                    | 90,91     |
 3 | Migrasi ODF to FTM                    | 92,22     |
14 | Migrasi ODF to FTM                    | 93,31     |

我只想选择两个数据,每个重复数据都这样存在:

ID | PERFORMANCE_INDICATOR                 | REALISASI |
    -----------------------------------------------------
 1 | Compliance of Inventory Data Consumer | 90,91     |
 7 | Compliance of Inventory Data Consumer | 92,22     |
 9 | Migrasi ODF to FTM                    | 90,91     |
 3 | Migrasi ODF to FTM                    | 92,22     |

我应该使用什么SQL语句查找那些行? 感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用分析型ROW_NUMBER,然后对其进行过滤:

with cte as (
    select your_table.*
           , row_number() over (partition by PERFORMANCE_INDICATOR order by ID) as rn
    from your_table
)
select ID, PERFORMANCE_INDICATOR, REALISASI
from cte
where rn <= 2
order by PERFORMANCE_INDICATOR, ID
/