使用 distinct on 连接多个表

Join on multiple tables using distinct on

  create table emp
(
   emp_id serial primary key,
   emp_no integer,
   emp_ref_no character varying(15),
   emp_class character varying(15)
);

create table emp_detail
(
   emp_detail_id serial primary key,
   emp_id integer,
   class_no integer,
   created_at timestamp without time zone,
   constraint con_fk foreign key(emp_id) references emp(emp_id)
 );

create table class_detail
(
class_id serial primary key,
emp_id integer,
class_no integer,
col1 JSONB,
created_at timestamp without time zone default now(),
constraint cd_fk foreign key(emp_id) references emp(emp_id)
);


INSERT INTO emp(
            emp_no, emp_ref_no, emp_class)
    VALUES ('548251', '2QcW', 'abc' );

INSERT INTO emp(
            emp_no, emp_ref_no, emp_class)
    VALUES ('548251', '2FQx', 'abc');

INSERT INTO emp(
            emp_no, emp_ref_no, emp_class)
    VALUES ('548251', '2yz', 'abc');

INSERT INTO emp_detail(
            emp_id, class_no, created_at
            )
    VALUES ( 1, 2, '2018-05-04 11:00:00'
            ); 

INSERT INTO emp_detail(
            emp_id, class_no, created_at
            )
    VALUES ( 1, 1, '2018-04-04 11:00:00'
            ); 

INSERT INTO emp_detail(
            emp_id, class_no, created_at
            )
    VALUES ( 2, 1, '2018-05-10 11:00:00' 
            );

INSERT INTO emp_detail(
            emp_id, class_no, created_at 
            )
    VALUES ( 2, 2, '2018-02-01 11:00:00' 
            );

INSERT INTO emp_detail(
            emp_id, class_no, created_at
            )
    VALUES ( 3, 2, '2018-02-01 11:00:00'
            );

insert into class_detail(emp_id, class_no, col1, created_at) values(1,1,'{"Name":"Nik"}', '2018-02-01 10:00:00');

insert into class_detail(emp_id, class_no, col1, created_at) values(1,1,'{"Name":"Nik Anderson"}', '2018-03-01 10:00:00');

insert into class_detail(emp_id, class_no, col1, created_at) values(1,2,'{"Name":"James Anderson TST"}', '2018-03-15 10:00:00');

insert into class_detail(emp_id, class_no, col1, created_at) values(1,2,'{"Name":"Tim Paine ST"}', '2018-04-01 10:00:00');

我要显示对应的emp_id,emp_no,emp_ref_no,class_no(最新的来自emp_detailtable创建于)以及 class_detail table 的所有列。 Class_detail table 应该显示最新对应的记录 class 没有

我希望看到的预期输出如下所示:-

emp id | emp_no | emp_ref_no |  class_no | class_id  |  class.col1          | class.created_at | class.created_by
1      | 548251 |  2QcW      |    2      |    4      |{"Name":"Tim Paine ST"}|2018-04-01 10:00:00| NUlL
2      | 548251 |  2FQx      |    1      |    2      |{"Name":"Nik Anderson"}|2018-03-01 10:00:00| NULL
3      | 548251 |  2yz       |    2      |    4      |{"Name":"Tim Paine ST"}|2018-04-01 10:00:00| NULL

正如我在评论中所说:这与 中的完全相同。您只需添加另一个加入和另一个 ORDER BY 组 (cd.created_at DESC)

demo:db<>fiddle

SELECT DISTINCT ON (ed.emp_id)
    e.emp_id, e.emp_no, e.emp_ref_no, ed.class_no, cd.*
FROM 
    emp_detail ed
JOIN emp e ON e.emp_id = ed.emp_id
JOIN class_detail cd ON ed.class_no = cd.class_no
ORDER BY ed.emp_id, ed.created_at DESC, cd.created_at DESC

注意:我不确定class_detail中的emp_id栏是干什么用的。它似乎设计得不好(这也是因为它在你的例子中总是1。)你应该检查你是否真的需要它。