4

背景

样本数据集

#Employee
Id | Period | Status 
---------------------
1  |  1 |   L    
1  |  2 |   G    
2  |  3 |   L    

我想要一个简单的选择查询来产生员工的最新记录(按期间),只有当状态='L'时。

结果将如下所示:

#Desired Results
Id | Period | Status | Sequence
-------------------------------
2  |  3     |   L    |   1

天真的尝试

显然,我对查询的天真尝试不起作用:

#select query
SELECT *, RANK() OVER (PARTITION BY id ORDER BY period ASC) sequence
FROM employees
WHERE   status = 'L' 
 AND    sequence = 1

结果如下:

#Naive (incorrect) Results
ID | Period | Status | Sequence
-------------------------------
1  |  1 |   L    |   1
2  |  3 |   L    |   1

知道在 SQL 中评估子句的顺序可以解释为什么它不起作用。以下是我的查询的评估方式:

  • 隔离 status='L' 的行
  • 对行进行排名
  • 隔离顶级行

我想要以下内容:

  • 对行进行排名
  • 隔离排名靠前的行
  • 隔离状态='L'

问题

  • 是否有可能——仅对 SELECT/WHERE 子句进行简单修改并仅使用基本谓词运算符——以确保基于 WHERE 子句中的分析函数的谓词在非聚合谓词之前得到评估?

  • 任何人都有其他可以作为最终用户在 Oracle Discoverer Plus 中实施的解决方案吗?

谢谢!

4

4 回答 4

4

是否可以在没有子查询的情况下执行此操作

从技术上讲,以下不是子查询,而是派生表

SELECT * 
FROM (
    SELECT *, 
           RANK() OVER (PARTITION BY id ORDER BY period ASC) sequence
    FROM employees
) t
WHERE status = 'L' 
  AND sequence = 1

我想不出一个不同的解决方案来解决你的问题。

于 2011-03-25T21:28:46.003 回答
0

我可能不得不做一个“多比”,把我的耳朵放在烤箱门上,为此熨烫我的手……

您可以创建一个评估当前行的函数。
请注意,这本质上是不可扩展的。但我想有总比没有好。

创建示例数据:

--drop table employee purge;

create table employee(
    id     number  not null
   ,period number  not null
   ,status char(1) not null
   ,constraint employee_pk primary key(id, period)
);

insert into employee(id,period, status) values(1, 1, 'L');
insert into employee(id,period, status) values(1, 2, 'G');
insert into employee(id,period, status) values(2, 3, 'L');

commit;

在数据库中创建最慢的函数:

create or replace function i_am_slow(
    ip_id     employee.id%type
   ,ip_period employee.period%type
)
return varchar2
as
   l_count number := 0;
begin
    select count(*)
      into l_count
      from employee e
     where e.id     = ip_id
       and e.period = ip_period
       and e.status = 'L'
       and not exists(
            select 'x'
              from employee e2
             where e2.id = e.id
               and e2.period > e.period);

    if l_count = 1 then
        return 'Y';
    end if;

    return 'N';
end;
/

演示函数的使用:

select id, period, status
  from employee
 where i_am_slow(id, period) = 'Y';

        ID     PERIOD STATUS
---------- ---------- ------
         2          3 L

冲向烤箱……

于 2011-03-25T23:35:49.360 回答
0

经典的 Group by

SELECT e.id, e.period, e.status, 1 sequence
FROM
(
    SELECT id, min(period) period
    FROM employees
    GROUP BY id
) X
JOIN employees e on e.period=X.period and e.id=X.id
WHERE e.status = 'L'

存在

select e.id, e.period, e.status, 1 sequence
FROM employees e
WHERE e.status = 'L'
  AND NOT EXISTS (select *
                  from employees e2
                  where e2.id=e.id and e2.period>e.period)
于 2011-03-25T22:04:57.100 回答
0

select * from (SELECT a.*, rank() OVER (ORDER BY period ASC) sequence from (select * from (select 1 id, 1 period, 'L' status from dual union all select 1 id, 2 period, 'G '双重联合的状态全部选择2个id,3个周期,'L'状态来自双重)其中状态='L')a)其中序列= 1

于 2021-12-30T05:35:33.183 回答