4

我有以下情况:

表员工:

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

我想要每个(名字,姓氏)1行,除非我们的姓氏中有一个空值,然后我只想要1行(名字,空值)

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

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 ;

谢谢 !

4

3 回答 3

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 的顺序。
于 2012-12-10T17:43:43.813 回答
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
于 2012-12-10T17:39:44.250 回答
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; 
于 2013-02-11T10:38:01.010 回答