Join on multiple tables using distinct on

2019-09-02 20:39发布

问题:

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

I want to display corresponding emp_id, emp_no, emp_ref_no, class_no(the latest one from emp_detail table based on created at)along with all the columns of class_detail table. Class_detail table should show the latest corresponding record of the class no

The expected output which I would like to see is something like below :-

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

回答1:

As I stated in the comments: It is exactly the same thing as in Inner join using distinct on. You simply have to add another join and another ORDER BY group (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

Note: I am not sure what the emp_id column in class_detail is for. It seems not well designed (this is also because it is always 1 in your example.) You should check whether you really need it.