仅选择计数为1的记录

时间:2011-11-02 01:37:14

标签: c# oracle

搜索高低,似乎无法找到如何做到这一点。我有一个有两列的表。

Order    route
=====    =====
Order_1   100
Order_1   101
Order_2   102
Order_3   103
Order_4   104

我想要结果,

Order_2   102
Order_3   103
Order_4   104

提前致谢。

1 个答案:

答案 0 :(得分:4)

Order是保留关键字,因此我改为使用order_column

首先是测试数据:

create table order_route_table(order_column varchar2(20)
    , route number);

insert into order_route_table 
select 'Order_1', 100 from dual
union all select 'Order_1', 101 from dual
union all select 'Order_2', 102 from dual
union all select 'Order_3', 103 from dual
union all select 'Order_4', 104 from dual;

传统上:

select order_column, min(route)
from order_route_table
group by order_column
having count(*) = 1;

使用分析:

select order_column, route
from (select order_column
        , route
        , count(*) over (partition by order_column) as order_count
    from order_route_table)
where order_count = 1;

两种情况都有结果:

ORDER_COLUMN              ROUTE
-------------------- ----------
Order_2                     102
Order_3                     103
Order_4                     104