我有心理障碍,希望有人在这里伸出援助之手。 表的DDL如下。
text_locate(text, "replacement")
#> text before instance after
#> 1 1 be advisd replacment coming tomorow
#> 2 2 …u get the email aobut the repalcement tomorro
#> 3 3 …been avdised of a posible replacement
#> 4 4 there is a replacement coming tomorrow
#> 5 5 what time tommorow is the replacment coming
表格中的数据如下。
CREATE TABLE TEST_QUERY
( APPROVER VARCHAR2(20 BYTE),
STEP VARCHAR2(20 BYTE),
APPROVED VARCHAR2(20 BYTE),
DOCUMENT VARCHAR2(20 BYTE)
)
对于此表中的每个文档,这些是按顺序排列的步骤列。它从1 ...无穷大。这里的关键是BK1和BK2条目将始终是具有最大步骤的表格中的条目。 我正在尝试编写的查询是一个查询,用于标识将所有较小步骤(按文档)设置为“Y”但Bk1和BK2仍为空的文档。
因此使用样本数据的此查询的预期输出将是。
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('JHON','1','Y','A1');
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('PAT','2','Y','A1');
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('FRANK','3','Y','A1');
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('BK1','4',null,'A1');
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('BK2','5',null,'A1');
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('JHON','1',null,'A2');
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('PAT','2',null,'A2');
Insert into TEST_QUERY (APPROVER,STEP,APPROVED,DOCUMENT) values ('BK1','3',null,'A2');
A2文档不会显示在此查询中,因为仍然存在小于设置为null的最大步骤的步骤。 关于如何完成这个的任何想法?
这是我到目前为止所做的事情。(这显然是关闭的)
Approver Step Document
BK1 4 A1
BK2 5 A1
感谢。
答案 0 :(得分:1)
您可以使用group by
:
select document
from test_query
group by document
having sum(case when approver in ('BK1', 'BK2') and approved is not null then 1 else 0 end) = 0 and
sum(case when approver not in ('BK1', 'BK2') and approved is not null then 1 else 0 end) = count(*) - 2;
如果您需要详细数据,则应将其与原始行组合使用:
select tq.*
from test_query tq
where approved is null and
document in (select document
from test_query
group by document
having sum(case when approver in ('BK1', 'BK2') and approved is not null then 1 else 0 end) = 0 and
sum(case when approver not in ('BK1', 'BK2') and approved is not null then 1 else 0 end) = count(*) - 2
);