[英]PSQL query to insert the records from one table to another based on condition
I have 2 Postgres tables with the following structure:我有 2 个具有以下结构的 Postgres 表:
Table "public.tmp"
Column | Type | Collation | Nullable | Default
-------------------+-------------------------+-----------+----------+---------
MY_SL | character varying(50) | | |
Release | character varying(50) | | |
HOST | character varying(50) | | not null |
UN NO. | character varying(50) | | |
STATUS | character varying(50) | | |
S_DATE | character varying(50) | | not null |
Table "public.mo"
Column | Type | Collation | Nullable | Default
-------------------+-------------------------+-----------+----------+-----------------------------------------
id | integer | | not null | nextval('mbss_output_id_seq'::regclass)
HOST | character varying(50) | | not null |
UN NO. | character varying(50) | | |
STATUS | character varying(50) | | |
S_DATE | character varying(50) | | not null |
compliant_status | character varying(50) | | not null |
Lets say I have data in the tmp table as below:假设我在 tmp 表中有如下数据:
Table: tmp
MY_SL | Release | HOST | UN NO.| STATUS | S_DATE
------------+-------------+-----------+----------------------+------------------
2 | 1 | RhelTest | 7:1:8 | COMPLIANT | 2020-08-26T15:16:48Z
12 | 1 | RhelTest | 7:1:9 | COMPLIANT | 2020-08-26T15:16:48Z
22 | 2 | RhelTest | 7:2:1 | COMPLIANT | 2020-08-26T15:16:48Z
4 | 1 | RhelTest | 7:2:10 | NC | 2020-08-26T15:16:48Z
11 | 2 | RhelTest | 7:2:11 | NC | 2020-08-26T15:16:48Z
1 | 3 | Demo1 | 7:2:11 | NC | 2020-08-26T15:16:48Z
23 | 3 | Demo1 | 7:2:11 | NC | 2020-08-26T15:16:48Z
333 | 3 | Demo2 | 7:2:11 | COMPLIANT | 2020-08-26T15:16:48Z
Now I want to write a psql INSERT INTO query which will copy the data from public.tmp to public.mo table and also perform below condition现在我想写一个 psql INSERT INTO 查询,它将把数据从 public.tmp 复制到 public.mo 表,并执行以下条件
When a Host has mix values in STATUS column, for eg if HOST: RhelTest当主机在 STATUS 列中具有混合值时,例如如果 HOST: RhelTest
Finally Expected output in the public.mo table:最后在 public.mo 表中的预期输出:
Table: public.mo
id | HOST | UN NO.| STATUS | S_DATE | compliant_status
------------+-------------+-----------+----------------------+------------------
1 | RhelTest | 7:1:8 | COMPLIANT | 2020-08-26T15:16:48Z | PARTIAL
2 | RhelTest | 7:1:9 | COMPLIANT | 2020-08-26T15:16:48Z | PARTIAL
3 | RhelTest | 7:2:1 | COMPLIANT | 2020-08-26T15:16:48Z | PARTIAL
4 | RhelTest | 7:2:10 | NC | 2020-08-26T15:16:48Z | PARTIAL
5 | RhelTest | 7:2:11 | NC | 2020-08-26T15:16:48Z | PARTIAL
6 | Demo1 | 7:2:11 | NC | 2020-08-26T15:16:48Z | NON_COMPLIANT
7 | Demo1 | 7:2:11 | NC | 2020-08-26T15:16:48Z | NON_COMPLIANT
8 | Demo2 | 7:2:11 | COMPLIANT | 2020-08-26T15:16:48Z | COMPLIANT
You can use window functions and a case
expression.您可以使用窗口函数和case
表达式。 Here is one way to do it, assuming that there are only two possible values for status
, as shown in your data:这是一种方法,假设status
只有两个可能的值,如您的数据所示:
insert in mo (host, un_no, status, s_date, compliant_status)
select host, un_no, status, s_date,
case
when min_host_status <> max_host_status then 'PARTIAL'
when min_host_status = 'NC' then 'NON_COMPLIANT'
else min_host_status
end
from (
select t.*,
min(status) over(partition by host) min_host_status,
max(status) over(partition by host) max_host_status
from tmp t
) t
The idea is to compare the minimum and maximum values of status
for each host
.这个想法是比较每个host
status
的最小值和最大值。 If there are different, then compliant_status
is "PARTIAL".如果有不同,则compliant_status
是“PARTIAL”。 Else, we turn "NC" to "NON_COMPLIANT", and leave the other value ("COMPLIANT") as it is.否则,我们将“NC”变为“NON_COMPLIANT”,并保持其他值(“COMPLIANT”)不变。
The subquery is not strictly necessary here, we could very well use the window functions directly in the outer query;子查询在这里不是绝对必要的,我们可以很好地直接在外部查询中使用窗口函数; I used it because it avoids repeating the same expressions again and again.我使用它是因为它避免了一次又一次地重复相同的表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.