postgresql Postgres -基于多列匹配从另一个表更新表

bxjv4tth  于 2023-06-22  发布在  PostgreSQL
关注(0)|答案(1)|浏览(150)

下面有两个表格,其中有样本数据。
表1:
| start_date timestamp|结束日期时间戳|t_no|导联ID|铅识别|铅b_id|
| - -----|- -----|- -----|- -----|- -----|- -----|
| 2023 - 01 - 06 19:14:04| 2023 - 01 - 06 21:00:05| PKT-19|二十三|||
| 2023 - 01 - 06 19:27:48| 2023 - 01 - 06 22:39:06| PKT-16|二十七|07|八十九|
| 2023 - 01 - 06 19:01:03| 2023 - 01 - 06 22:47:00| PKT-21|五十六|十一|23,78|
| 2019 - 02 - 22 01:01:09| 2019 - 05 - 22 01:01:01| PKT-45|六十七|十个||
| 2023 - 01 - 06 18:18:54| 2023 - 01 - 06 18:31:46| PKT-32| 07|十四|九十七|
| 2023 - 01 - 06 19:18:54||PKT-35|十六|07|十四|
表2:
| 导联ID|日期时间戳|pkt型|t_no|
| - -----|- -----|- -----|- -----|
| 07| 2023 - 01 - 06 19:28:48|||
| 七十八|2023 - 01 - 06 22:46:00|||
| 07| 2023 - 01 - 06 18:31:42|||
| 二十三|2023 - 01 - 06 21:00:04|||
| 六十七|2022 - 12 - 02 05:30:09|||
| 十六|2023 - 01 - 06 19:18:55|||
我需要帮助使用以下条件更新Table_2列(pkt_type,t_no)-

  • 表_2中的lead_id应与表_1中的lead_id、lead_a_id或lead_b_id匹配。
  • 表2中的datestamp应该介于或等于表1中的start_datestamp & end_datestamp之间。
  • 如果以上条件匹配,则更新Table_2 pkt_type ="Not Lead ',如果表2中lead_id与lead_a_id或lead_b_id匹配,否则更新Table_2 pkt_type =" Lead',如果表2中lead_id与表1 lead_id匹配,则更新Table_2 pkt_type ="Lead '。
  • 此外,更新t_no。

输出:
| 导联ID|日期时间戳|pkt型|t_no|
| - -----|- -----|- -----|- -----|
| 07| 2023 - 01 - 06 19:28:48|非电极导线|PKT-16|
| 七十八|2023 - 01 - 06 22:46:00|非电极导线|PKT-21|
| 07| 2023 - 01 - 06 18:31:42|铅|PKT-32|
| 二十三|2023 - 01 - 06 21:00:04|铅|PKT-19|
| 六十七|2022 - 12 - 02 05:30:09|||
| 十六|2023 - 01 - 06 19:18:55|||
设置表查询-

CREATE TABLE table_1 (
start_datetimestamp timestamp without time zone NOT NULL,
end_datetimestamp timestamp without time zone,
t_no character varying(10) NOT NULL,
lead_id character varying(2) NOT NULL,
lead_a_id character varying(2),
lead_b_id character varying(5),PRIMARY KEY(start_datetimestamp, t_no, lead_id));
INSERT INTO table_1 VALUES
      ('2023-01-06 19:14:04', '2023-01-06 21:00:05', 'PKT - 19', '23', '', '')
    , ('2023-01-06 19:27:48', '2023-01-06 22:39:06', 'PKT - 16', '27', '07', '89')
    , ('2023-01-06 19:01:03', '2023-01-06 22:47:00', 'PKT - 21', '56', '11', '23,78')
    , ('2022-12-02 05:45:09', '2022-12-02 05:54:41', 'PKT - 45', '67', '10', '')
    , ('2023-01-06 18:18:54', '2023-01-06 18:31:46', 'PKT - 32', '07', '14', '97')
    , ('2023-01-06 19:18:54', NULL, 'PKT - 35', '16', '07', '14')
    ;

CREATE TABLE table_2 (
lead_id character varying(2) NOT NULL,
datetimestamp timestamp without time zone NOT NULL,
pkt_type character varying(10) NOT NULL,
t_no character varying(10) NOT NULL,PRIMARY KEY(datetimestamp, lead_id));

INSERT INTO table_2 VALUES
      ('07', '2023-01-06 19:28:48', '', '')
    , ('78', '2023-01-06 22:46:00', '', '')
    , ('07', '2023-01-06 18:31:42', '', '')
    , ('23', '2023-01-06 21:00:04', '', '')
    , ('67', '2022-12-02 05:30:09', '', '')
    , ('16', '2023-01-06 19:18:55', '', '')
    ;

测试环境-https://dbfiddle.uk/bwdeYkjD

8aqjt8rx

8aqjt8rx1#

尝试使用如下2个update语句:

update table_2
set pkt_type='Lead', t_no=table_1.t_no
FROM table_1
WHERE table_2.lead_id = table_1.lead_id
AND table_2.datetimestamp between table_1.start_datetimestamp 
AND table_1.end_datetimestamp;

update table_2
set pkt_type='Not Lead', t_no=table_1.t_no
FROM table_1
WHERE table_2.lead_id != table_1.lead_id 
  and table_2.datetimestamp between table_1.start_datetimestamp 
  and table_1.end_datetimestamp 
  AND (table_2.lead_id = table_1.lead_a_id
  OR table_2.lead_id = table_1.lead_b_id);

相关问题