首页 > 解决方案 > 使用 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_detail 表中基于 created at 的最新一个)以及 class_detail 表的所有列。Class_detail 表应显示该类的最新对应记录 no

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

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

标签: postgresqljoindistinct-on

解决方案


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

演示:db<>小提琴

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

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


推荐阅读