Sql 获取具有null的第一行

Sql 获取具有null的第一行,sql,postgresql,Sql,Postgresql,我有一个列的表格 drop TABLE "public"."opportunities"; CREATE TABLE "public"."opportunities" ( "itemcode" VARCHAR COLLATE "default", "creationtime" TIMESTAMPTZ(6) NOT NULL, "finishtime" TIMESTAMPTZ(6), "ordertag" INT8,

我有一个列的表格

    drop TABLE "public"."opportunities";
CREATE TABLE "public"."opportunities" ( 
     "itemcode"     VARCHAR COLLATE "default", 
     "creationtime" TIMESTAMPTZ(6) NOT NULL, 
     "finishtime"   TIMESTAMPTZ(6), 
     "ordertag"     INT8, 
     "trackingblob" VARCHAR
); 
insert into opportunities VALUES ('1', now(), null, NULL, 'blob1');
insert into opportunities VALUES ('1', now(), null, 2, 'blob2');
insert into opportunities VALUES ('1', now(), null, null, 'blob3');
insert into opportunities VALUES ('2', now(), null, NULL, 'blob1a');
insert into opportunities VALUES ('2', now(), null, 2, 'blob2a');
insert into opportunities VALUES ('2', now(), null, null, 'blob3a');
预期结果将是包含blob1和blob1a的行

我想为每个orderTag获取orderTag为null的前一个条目的跟踪blob——时间戳之后可能有行,也应该忽略这些行。 这在一个SQL中是可能的,还是我需要编写一个包含两个步骤的程序

select * from opportunities op1
where (select ordertag from opportunities op2
       where op2.creationtime =
           (select min(creationtime) from opportunities op3
            where op3.creationtime > op1.creationtime)) is not null

也就是说,返回下一行中的一行(creationtime方面)有一个不为空的ordertag。

您能添加一些样本数据和预期结果(使用该样本数据)吗?我已根据请求添加了一个样本。如果blob3有一个ordertag怎么办?是否应该返回blob2,或者仅仅是blob1?当与上面的sql语句一起运行时,我没有返回任何行-我还错过了grouping by order标记。当我测试它时,它返回了预期的行。也许是时间上的差异?放慢插入速度,看看是否有什么不同?