使用多个rownumbers SQL过滤掉记录

时间:2016-02-01 19:29:02

标签: sql oracle

是否可以使用ROWNUMBER()过滤掉具有多个rownumber的记录。

E.g。

  • 包含只有一行的记录?

  • 排除多个记录?
到目前为止

代码:

SELECT * 
FROM 
    (SELECT 
         ID, blah, blah2, 
         ROWNUMBER() OVER(PARTITION BY ID ORDER BY blah, blah2) AS rn 
     ORDER BY ID) 
WHERE rn IS ONLY 1;

1 个答案:

答案 0 :(得分:0)

实际上你做得几乎是正确的:

SELECT * 
FROM 
    (SELECT 
         ID, blah, blah2, 
         ROW_NUMBER() OVER(PARTITION BY ID ORDER BY blah, blah2) AS rn
     FROM table_name
     WHERE ... 
    ) 
WHERE rn = 1;