当前位置:网站首页>How to remove duplication in left join from a simple example
How to remove duplication in left join from a simple example
2022-06-22 01:15:00 【Summer seven】
1. Execute statement , establish 3 A watch
CREATE TABLE table_dev1 (
id serial NOT NULL,
devicecode text NULL, -- equipment id
openflag int4 NULL -- Whether to start 1: start-up , 0: Not activated
);
CREATE TABLE table_dev2 (
id serial NOT NULL,
devicecode text NULL, -- equipment id
devicetype int4 NULL, -- Device type
vendor text NULL -- Name of manufacturer
);
CREATE TABLE table_dev3 (
id serial NOT NULL,
devicecode text NULL, -- equipment id
openflag int4 NULL, -- Whether to start 1: start-up , 0: Not activated
devicetype int4 NULL, -- Device type
vendor text NULL -- Name of manufacturer
);
2. Insert data into table_dev1 & table_dev2
insert into table_dev1 (devicecode, openflag)values ('123', 1);
insert into table_dev1 (devicecode, openflag)values ('abc', 0);
insert into table_dev2 (devicecode, devicetype, vendor)values ('123', 100, ' Passerby ');
insert into table_dev2 (devicecode, devicetype, vendor)values ('123', 100, ' Passerby ');
insert into table_dev2 (devicecode, devicetype, vendor)values ('abc', 101, ' Unknown ');
Get table data 
3. Use left join Association table table_dev1 & table_dev1, And insert the data into the table table_dev3
1) Not to heavy
insert into table_dev3 (devicecode, openflag, devicetype, vendor)
select t1.devicecode, t1.openflag, t2.devicetype, t2.vendor
from table_dev1 t1
left join
table_dev2 t2
on t1.devicecode = t2.devicecode
Get the results >>
2) duplicate removal
insert into table_dev3 (devicecode, openflag, devicetype, vendor)
select t1.devicecode, t1.openflag, t2.devicetype, t2.vendor
from table_dev1 t1
left join
(select t3.* from
(select devicecode,devicetype,vendor,row_number() over (partition by devicecode order by id desc) as rownum from table_dev2) t3 where t3.rownum = 1 ) t2
on t1.devicecode = t2.devicecode
Get the results >>
边栏推荐
猜你喜欢

Broadening - simple strategy test

Pytorch learning 08: splicing and splitting

【DailyFresh】课程记录
![Sum of four numbers [array sort + double pointer]](/img/9e/e1932120a9b69847898e89c45e2eb4.png)
Sum of four numbers [array sort + double pointer]

2. add two numbers

Tensorflow环境搭建

03 fastjson resolving circular references
![[dailyfresh] course record 3 -- product search related](/img/54/e1dbc6c2a6bd6d1f39de0f55d79976.png)
[dailyfresh] course record 3 -- product search related

Error 4 opening dom ASM/Self in 0x8283c00

利用SSM框架实现用户登陆
随机推荐
BigDecimal基本使用
div设置滚动和监听滚动距离
Summary of new MySQL 8.0 features
[GStreamer] 插件编写 —— Test程序
FLowable运行时事务相关的表和表结构
编译原理-递归下降子程序法
让人无法喜爱的STL
pytorch学习02:手写数字识别
Handwriting database connection pool
4273. 链表合并
isnull() ifnull() nullif()
从简单实例来看 left join 如何去重
SQL语句——权限管理
Li Kou daily question - day 24 -485 Maximum number of consecutive 1
【环境踩坑】在自己电脑上搭建FastDFS
4274. suffix expression
Pytorch learning 09: basic matrix operations
Simple analysis of three differences between cross-border trade and cross-border E-commerce
Using SSM framework to realize user login
Install easyx-vc2019