如何在数据库中获取每个主题的最小和最大日期

How to get min & max date across the database for each subject

我有一个 psotgresql 数据库,它有 4 个 table,每个 table 都有一个日期列。

Table 1

person_id       meas_date

  1            2007/02/11
  2            2008/05/13  
  3            2008/07/29
  5            2006/03/21

Table 2

person_id     visit_date

  1            2003/06/21
  2            2005/02/23  
  3            2006/04/19
  5            2004/06/11

Table 3

person_id     condition_date

  1            2008/06/21
  2            2009/02/23  
  3            2005/04/19
  5            2002/06/11

Table 4

person_id       d_date

  1            2018/06/21
  2            2005/02/23  
  3            2004/04/19
  5            2009/06/11

目前我正在做类似下面的事情来从一个 table 中找到它,但是我如何在我的数据库中找到所有 tables。在这种情况下,它是 4 tables

select 
  person_id,
  min(condition_start_date) as min_date,
  max(condition_start_date) as max_data,
from Table_3
group by person_id

但是你能帮我在所有 table 中找到一个 subject/person_id 吗?

我希望我的输出如下所示

person_id       max_date      min_date

  1            2018/06/21    2003/06/21
  2            2009/02/23    2005/02/23
  3            2006/04/19    2004/04/19
  5            2009/06/11    2002/06/11

使用union all和聚合:

select person_id, min(date), max(date)
from ((select person_id, date from table1) union all
      (select person_id, date from table2) union all
      (select person_id, date from table3) union all
      (select person_id, date from table4) 
     ) pd
group by person_id;