如何在postgresql中使用procedure_fk过滤器在postgres中查找最新版本更新的datetime和最早的修订日期时间

时间:2013-06-03 13:48:30

标签: sql postgresql postgresql-9.1 postgresql-9.2

这些是我的表格我应该在postgres中找到最新版本更新的日期时间和最早的修订日期时间与procedure_fk过滤器

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

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
);

1 个答案:

答案 0 :(得分: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,这个答案是根据我的经验而得出的。