-1

这些是我的表,我应该使用 procedure_fk 过滤器在 postgres 中找到最新的修订更新日期时间和最旧的修订日期时间

任何人都可以帮助我提前谢谢

CREATE TABLE "study" (
"pk" SERIAL    PRIMARY KEY,
"procedure_runtime_fk" BIGINT,
"patient_fk" BIGINT,
"modality_infra_fk" BIGINT,
"priority_fk" BIGINT,
"status_fk" BIGINT,
"pacs_server_fk" BIGINT,
"study_iuid" VARCHAR(1024)   UNIQUE,
"study_datetime" TIMESTAMP,
"accession_no" VARCHAR(128),
"study_desc" TEXT,
"mods_in_study" TEXT,
"num_series" BIGINT,
"num_instances" BIGINT,
"availibility" VARCHAR(32),
"ref_physician" VARCHAR(255),
"create_datetime" TIMESTAMP,
"childs" TEXT
);

CREATE TABLE "procedure_runtime_information" (
"pk" SERIAL    PRIMARY KEY,
"patient_fk" BIGINT,
"patient_visit_fk" BIGINT,
"procedure_fk" BIGINT,
"procedure_performed_datetime" TIMESTAMP,
"author_fk" BIGINT,
"creation_datetime" TIMESTAMP,
"procedure_actual_duration" BIGINT,
"procedure_indications" TEXT DEFAULT NULL,
"pre_procedure_info" TEXT DEFAULT NULL,
"procedure_description" TEXT DEFAULT NULL,
"procedure_exposure" TEXT DEFAULT NULL,
"procedure_skindose" TEXT DEFAULT NULL,
"ref_phys_fk" BIGINT DEFAULT NULL,
"object_type" BIGINT DEFAULT NULL,
"priority_fk" BIGINT DEFAULT NULL,
"procedure_id" VARCHAR(256) DEFAULT NULL,
"patient_arrival_datetime" TIMESTAMP,
"procedure_start_datetime" TIMESTAMP

);


CREATE TABLE "report_history" (
"pk" SERIAL    PRIMARY KEY,
"revision" BIGINT,
"report_fk" BIGINT,
"old_status_fk" BIGINT,
"updatedby_fk" BIGINT,
"updated_datetime" TIMESTAMP,
"file_path" TEXT,
"synopsis" TEXT
);



CREATE TABLE "report" (
"pk" SERIAL    PRIMARY KEY,
"report_uuid" VARCHAR(32)   UNIQUE,
"study_fk" BIGINT,
"status_fk" BIGINT,
"priority_fk" BIGINT,
"report_relative_path" VARCHAR(256),
"report_type_fk" BIGINT,
"createdby_fk" BIGINT,
"created_datetime" TIMESTAMP
);
4

1 回答 1

1

我不完全理解你在这里想要做什么,但你的关系对我来说并不完全清楚。但是,您应该能够根据最小值和最大值进行过滤。例如,如果您有以下情况:

Create table foo(
id serial, 
foo_detail text);

create table foo_history
(id serial, 
foo_id int references foo(id),
some_new_info text,
modified timestamp with time zone NOT NULL DEFAULT now()
);

create index idx_foo_history_modified on foo_history using btree (modified)

(select * from foo f
inner join foo_history fh on (fh.foo_id = f.id)
where f.id = 3
order by fh.modified asc
limit 1)
UNION ALL
(select * from foo f
inner join foo_history fh on (fh.foo_id = f.id)
where f.id = 3
order by fh.modified desc
limit 1);

您也可以使用子查询来执行此操作,如下所示,但我相信联合会更有效率。

select * from foo f
inner join foo_history fh on (fh.foo_id = f.id)
where f.id = 3
and fh.id = (select max(id) from foo_history where foo_id = 3)
or fh.id = (select min(id) from foo_history where foo_id = 3);

希望这可以帮助。顺便说一句,您应该包括您正在使用的 Postgresql 版本。我使用 9.1,这个答案是根据我的经验得出的。

于 2013-06-03T16:46:08.397 回答