使用不同的on联接多个表 [英] Join on multiple tables using distinct on

查看:119
本文介绍了使用不同的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表基于创建于的最新值)以及class_detail表的所有列. Class_detail表应显示类别

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

推荐答案

正如我在评论中所述:与

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<>小提琴

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.)您应检查是否确实需要它.

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.

这篇关于使用不同的on联接多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆