繁体   English   中英

如果列中不存在,则通过选择与一列不同来插入行记录

[英]Insert row records by selecting distinct from one column if not present in column

问题:我有如下表,我想为所有列插入类似的行,但联系人类型除外,其中每个数字列(如(permission_volume、volume_last_month 等)将为 0 并且联系人类型将具有不同的值,所以我有 contact_type (数字, No_Permission, Email, Post, SMS, Phone) 并且我想插入在contact_type列中不可用的所有其他联系人类型

+----------------+-------+---------------+-------------------+--------------+-------------------+------------+------------------+----------------+-------------------+--------------------------+---------------------------+-----------------------+
|     market     | brand | contact_type  | permission_volume | custom_group |   consent_type    |   period   | calculation_type |     region     | volume_last_month | avg_last_3_months_volume | this_month_last_yr_volume | total_customer_volume |
+----------------+-------+---------------+-------------------+--------------+-------------------+------------+------------------+----------------+-------------------+--------------------------+---------------------------+-----------------------+
| United Kingdom | LR    | Post          |                 1 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |                 1 |                        2 |                         2 |                     3 |
| United Kingdom | LR    | No_Permission |             10028 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |             12888 |                    11641 |                         4 |                     5 |
| United Kingdom | JG    | Digital       |                 1 | Active Owner | PR                | 2022-08-01 | cumulative       | United Kingdom |                 1 |                        2 |                         2 |                     3 |
+----------------+-------+---------------+-------------------+--------------+-------------------+------------+------------------+----------------+-------------------+--------------------------+---------------------------+-----------------------+

预期 Output:

+----------------+-------+---------------+-------------------+--------------+-------------------+------------+------------------+----------------+-------------------+--------------------------+---------------------------+-----------------------+
|     market     | brand | contact_type  | permission_volume | custom_group |   consent_type    |   period   | calculation_type |     region     | volume_last_month | avg_last_3_months_volume | this_month_last_yr_volume | total_customer_volume |
+----------------+-------+---------------+-------------------+--------------+-------------------+------------+------------------+----------------+-------------------+--------------------------+---------------------------+-----------------------+
| United Kingdom | LR    | Post          |                 1 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |                 1 |                        2 |                         2 |                     3 |
| United Kingdom | LR    | No_Permission |             10028 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |             12888 |                    11641 |                         4 |                     5 |
| United Kingdom | LR    | Digital       |                 0 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | LR    | Email         |                 0 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | LR    | SMS           |                 0 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | LR    | Phone         |                 0 | Lapsed Leads | Owner and Vehicle | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | JG    | Digital       |                 1 | Active Owner | PR                | 2022-08-01 | cumulative       | United Kingdom |                 1 |                        2 |                         2 |                     3 |
| United Kingdom | JG    | Phone         |                 0 | Active Owner | PR                | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | JG    | Email         |                 0 | Active Owner | PR                | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | JG    | SMS           |                 0 | Active Owner | PR                | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | JG    | No_Permission |                 0 | Active Owner | PR                | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
| United Kingdom | JG    | Post          |                 0 | Active Owner | PR                | 2022-08-01 | cumulative       | United Kingdom |                 0 |                        0 |                         0 |                     0 |
+----------------+-------+---------------+-------------------+--------------+-------------------+------------+------------------+----------------+-------------------+--------------------------+---------------------------+-----------------------+

试过这个,但它不工作:

  declare i int64 default 1;
declare count_of_markets int64;
execute immediate "select count(distinct market)  from `table1`" into count_of_markets;


CREATE OR REPLACE TABLE  `lookup_table`  (
  id int64 not null, contact_type STRING NOT NULL, brand string NOT NULL 
);
while i <= count_of_markets do  
INSERT INTO `lookup_table`  
    (id, contact_type,brand)
VALUES 
  (i,"Email","JG"),
  (i,"SMS","JG"),
  (i,"Phone","JG"),
  (i,"Digital","JG"),
  (i,"No_Permission","JG"),
  (i,"Post","JG"),
  (i,"Email","LR"),
  (i,"SMS","LR"),
  (i,"Phone","LR"),
  (i,"Digital","LR"),
  (i,"No_Permission","LR"),
  (i,"Post","LR");
 set i = i+1; 
 end while;


create or replace table `table1`  
partition by period as
select * from(

with cte_market as (select 
    *
  , ROW_NUMBER() OVER(ORDER BY market) as id
from (
select distinct market from 
  `table1`)),
contact_CTE as (select * from `lookup_table`),
step3 as (select cte_market.market, contact_CTE.brand,contact_CTE.contact_type from cte_market left join contact_CTE 
on cte_market.id=contact_CTE.id)


select a.market as market,  a.brand as brand,   a.contact_type as contact_type, a.permission_volume as permission_volume,   a.custom_group as custom_group, a.consent_type as consent_type, a.period as period, a.calculation_type as calculation_type, a.region as region, a.volume_last_month as volume_last_month,   a.avg_last_3_months_volume as avg_last_3_months_volume, a.this_month_last_yr_volume as this_month_last_yr_volume,   a.total_customer_volume as total_customer_volume
 from `table1`  a  full outer join step3
 on a.market=step3.market
 and a.brand=step3.brand
 and a.contact_type=step3.contact_type)

我改变了解决问题的方法,它奏效了。

create or replace table `table1` 
partition by period as 
with step_one as (select distinct market, brand, custom_group ,consent_type , period as period, calculation_type,   region  from `table1`),
step_two as (select * from `table1`),
step_three as (
select distinct * from (select a.*, b.contact_type
from step_one a
left join step_two b
on a.market = b.market
and a.brand = b.brand
and a.custom_group = b.custom_group
and a.period = b.period
and a.calculation_type = b.calculation_type
and a.region = b.region))


select market, brand, contact_type, permission_volume, custom_group, consent_type,  period, calculation_type,   region, volume_last_month,  avg_last_3_months_volume, this_month_last_yr_volume, total_customer_volume
from
(select a.* , b.permission_volume as permission_volume, b.volume_last_month as volume_last_month,   b.avg_last_3_months_volume as avg_last_3_months_volume, b.this_month_last_yr_volume as this_month_last_yr_volume,   b.total_customer_volume as total_customer_volume  
from step_three a
left join step_two b
on a. market = b.market
and a.brand = b.brand
and a.contact_type = b.contact_type
and a.custom_group = b.custom_group
and a.consent_type = b.consent_type
and a.period = b.period
and a.calculation_type = b.calculation_type
and a.region = b.region)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM