SQL基于匹配点ID的匹配2 ID从一个表A更新到表B

时间:2019-03-27 13:37:13

标签: sql vertica

尝试将值从表(first_stage)更新为表(final),这两个表具有相同的字段名但值不同,表内容为:

(Min_Date) date, 
(Max_Date) date, 
(NoofDays) int, 
(IMSI) string, 
(Site) string, 
(Down_Link) int, 
(Up_Link) int, 
(Connection) int 

基于IMSI和站点,如果它存在于表行中,则将最小日期作为Min_Date,将最大日期作为Max_Date并获得

min(Min_Date),max(Max_Date)sum(NoofDays),sum(Down_Link),sum(up_Link),sum(connection)

,并且如果两个行ID都不匹配(IMSI,站点)与表(最终),则将该行插入最终表。我仍然是sql的新手

table first_stage:
MinDate    Max_Date  NoofDays  IMSI  Site   Down_link  Up_link  Connection
2019-03-22 2019-03-26   1       222  google    1        1         1
2019-03-26 2019-03-27   3       222  youtube   1        1         1
2019-03-02 2019-03-27   5       333  facebook  2        3         1
2019-03-02 2019-03-27   5       111  facebook  20       33        11



table final:
MinDate    Max_Date  NoofDays  IMSI  Site   Down_link  Up_link  Connection
2019-03-01 2019-03-27   1       222  google    2        2         1
2019-03-12 2019-03-25   1       222  youtube   2        2         2
2019-03-25 2019-03-27   4       333  facebook  3        6         1

它必须与IMSI和站点都匹配才能做出更新声明,更新后的最终表必须如下图所示:

table final:
MinDate    Max_Date  NoofDays  IMSI  Site   Down_link  Up_link  Connection
2019-03-01 2019-03-27   2       222  google    3        3         2
2019-03-12 2019-03-27   4       222  youtube   3        3         3
2019-03-02 2019-03-27   9       333  facebook  5        9         2
2019-03-02 2019-03-27   5       111  facebook  20       33        11

1 个答案:

答案 0 :(得分:1)

我从未使用过vertica,但我认为这可能有效:

MERGE 
INTO FINAL 
USING FIRST_STAGE
ON IMSI = FIRST_STAGE.IMSI and Site = FIRST_STAGE.Site
WHEN MATCHED THEN UPDATE SET
  Min_Date = least(FIRST_STAGE.Min_Date, Min_Date),
  Max_Date = greatest(FIRST_STAGE.Max_Date, Max_Date),
  NoofDays = FIRST_STAGE.NoofDays + NoofDays,
  Down_Link = FIRST_STAGE.Down_Link + Down_Link,
  up_Link = FIRST_STAGE.up_Link + up_Link,
  connection = FIRST_STAGE.connection + connection
WHEN NOT MATCHED THEN INSERT ( Min_Date, 
  Max_Date, 
  NoofDays, 
  IMSI, 
  Site, 
  Down_Link, 
  Up_Link, 
  Connection )
VALUES ( FIRST_STAGE.Min_Date, 
  FIRST_STAGE.Max_Date, 
  FIRST_STAGE.NoofDays, 
  FIRST_STAGE.IMSI, 
  FIRST_STAGE.Site, 
  FIRST_STAGE.Down_Link, 
  FIRST_STAGE.Up_Link, 
  FIRST_STAGE.Connection )