简体   繁体   English

PSQL查询根据条件将记录从一张表插入到另一张表

[英]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

  • has 2 values in STATUS column as 'COMPLIANT' and 'NC' then the column compliant_status should have the value 'PARTIAL' for such rows在 STATUS 列中有 2 个值作为“COMPLIANT”和“NC”,那么对于这些​​行,列 compliant_status 应该具有值“PARTIAL”
  • or if there is only one value like 'COMPLIANT' then the column compliant_status should have the value 'COMPLIANT' for such rows或者如果只有一个像“COMPLIANT”这样的值,那么对于这样的行,列 compliant_status 应该具有值“COMPLIANT”
  • or if there is only one value like 'NC' then the column compliant_status should have the value 'NON_COMPLIANT' for such rows或者,如果只有一个像“NC”这样的值,那么对于这些​​行,列 compliant_status 应该具有值“NON_COMPLIANT”

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.

相关问题 如果不插入特定值,则根据条件从另一个表插入数据 - Insert data from another table based on condition, if not insert specific value PSQL Select 基于条件的一个表中的所有记录,而不是另一个表中的所有记录 - Select all records from one table not in another table based on a condition SQL查询-根据一个条件从表中获取行,并根据另一条件通过连接表获取行 - SQL query - get rows from a table based on one condition and through join table based on another condition 根据条件从另一个表插入图像 - Insert Image from another table based on condition 访问查询以插入另一个表中的记录 - access query to insert records from another table 根据where条件将一个表中的一列的值插入到另一个表中 - Insert value of one column from one table to another table based on where condition 根据另一个表中的记录选择一个表中的记录 - Select records from one table based on records from another table MySQL查询基于另一个表的条件 - MySQL query based on condition from another table Oracle SQL:根据条件将某些记录从一个表转移到另一个过滤行 - Oracle SQL: Transfer certain records from one table to another filtering rows based on condition 如何根据来自另一个表的信息在表中插入新记录? - How to insert into a table new records based on info from another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM