【问题标题】:how to find the latest revisions updated datetime and oldest revisions datetime in postgres with procedure_fk filter in postgresql [closed]如何使用postgresql中的procedure_fk过滤器在postgres中查找最新修订更新日期时间和最旧修订日期时间[关闭]
【发布时间】:2013-06-03 13:48:29
【问题描述】:

这些是我的表格,我应该使用 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
);

【问题讨论】:

  • 我需要在procedure_runtime_information表中找到给定procedure_fk的最新版本updated_datetime和最旧版本updated datetime
  • 谁能帮忙解决这个问题

标签: sql postgresql postgresql-9.1 postgresql-9.2


【解决方案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,这个答案是根据我的经验得出的。

【讨论】:

  • select * from ( select max(h.updated_datetime) as max, min(h.updated_datetime) as min from report r, report_history h, procedure_runtime_information PRI, study S where h.report_fk=r.pk and r.study_fk=S.pk and PRI.pk=S.procedure_runtime_fk and extract(epoch from (max(h.updated_datetime) - min(h.updated_datetime) )
猜你喜欢
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 2020-11-19
相关资源
最近更新 更多