我有一个包含列
的表格 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,前一个条目的跟踪blob与OrderTag为null - 时间戳之后可能还有行也应该被忽略。 这可能在一个SQL中,还是我需要用两个步骤编写程序?
答案 0 :(得分:0)
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-wise)有一个非空的ordertag。