SQL OVER(Partition by) - 处理空值

时间:2012-12-10 17:21:23

标签: sql oracle null

我有以下情况:

表员工:

First Name | Last Name | Department | Salary
-----------|-----------|------------|---------
John       | Doe       | Finance    | 20
John       | Doe       | R&D        | 20
John       | null      | Finance    | 20
John       | long      | Finance    | 20

我想要每行1个(名字,姓氏), 除非我们在姓氏中有一个null,然后我只想要一行(First Name,null)

对于上面的例子,结果是:

First Name | Last Name | Department | Salary
-----------|-----------|------------|---------
John       | null      | Finance    | 20

但是如果我没有那个记录那么结果应该是:

First Name | Last Name | Department | Salary
-----------|-----------|------------|---------
John       | Doe       | R&D        | 20
John       | long      | Finance    | 20

我猜答案涉及到一些Partition By-s,但我不确定在哪里。

现在我来到这里:

SELECT FirstName,LastName, DEPARTMENT,Salary,RK FROM 
(
select * from 
    SELECT EXT.*, 
    ROW_NUMBER() OVER(PARTITION BY EXT.FirstName,EXT.LastName 
    ORDER BY rownum ASC) AS RK
      FROM Employees EXT   
)
WHERE RK = 1 ;

谢谢!

3 个答案:

答案 0 :(得分:8)

您的问题出在PARTITION子句中。除非至少有一个具有该名字的姓氏为NULL,否则您需要姓氏中的每个名字,在这种情况下,您只需要那些具有NULL姓氏的名字。

这里的答案是使用RANK()而不是ROW_NUMBER()。 RANK()不会创建连续列表;相反,具有相同值的行获得相同的排名。

select firstname, lastname, department, salary, rk 
  from ( select a.*
              , rank() over ( partition by firstname
                                  order by case when lastname is null then 0 
                                                else 1 
                                           end
                                           ) as rnk
           from employees a
                )
where rnk = 1

这可以通过使姓氏相关而不是姓氏本身来存在。

还有两点:

  1. 您有一个没有括号的嵌套选择。这不起作用。
  2. ROWNUM没有点订购。根据定义,rownum按语句返回的顺序返回行,这意味着始终的行将按照ROWNUM的顺序返回。

答案 1 :(得分:2)

类似的东西:

SQL> create table person
  2  (
  3    fname varchar2(10),
  4    lname varchar2(10),
  5     dept  varchar2(10),
  6     sal   number
  7  );

Table created.

SQL> insert into person values ('John', 'Doe', 'Finance', 20);

1 row created.

SQL> insert into person values ('John', 'Doe', 'R&D', 20);

1 row created.

SQL> insert into person values ('John', '', 'Finance', 20);

1 row created.

SQL> insert into person values ('John', 'Long', 'Finance', 20);

1 row created.

SQL> insert into person values ('Paul', 'Doe', 'R&D', 30);

1 row created.

SQL> insert into person values ('Paul', 'Doe', 'Finance', 30);

1 row created.

SQL> insert into person values ('Paul', 'Long', 'Finance', 30);

1 row created.

SQL> select fname, lname, dept, sal
  2    from (select fname, lname, dept, sal,has_null,
  3                  row_number() over(partition by fname,
  4                                                  case when has_null = 'N' then lname else null end
  5                                    order by lname desc nulls first) rn
  6             from (select fname, lname,
  7                           nvl(max(case  when lname is null then 'Y'
  8                               end) over(partition by fname), 'N') has_null, dept, sal
  9                      from person))
 10   where rn = 1;

FNAME      LNAME      DEPT              SAL
---------- ---------- ---------- ----------
John                  Finance            20
Paul       Doe        R&D                30
Paul       Long       Finance            30

答案 2 :(得分:0)

该查询执行(相同)技巧,但预先形成更好。

SELECT fname, 
       lname, 
       dept, 
       sal 
FROM   (SELECT fname, 
               lname, 
               dept, 
               sal, 
               First_value(lname) 
                 OVER( 
                   partition BY fname 
                   ORDER BY lname nulls first) null_domain, 
               Row_number() 
                 OVER ( 
                   partition BY fname, lname 
                   ORDER BY fname)             r 
        FROM   person) 
    WHERE  ( ( null_domain IS NULL 
           AND lname IS NULL ) 
          OR null_domain IS NOT NULL ) 
        AND r = 1;