Warm tip: This article is reproduced from stackoverflow.com, please click
amazon-redshift amazon-web-services aws-glue sql

Insert values in a table based on entries of other table in Redshift

发布于 2020-04-23 12:19:12

I have two tables in AWS Redshift and need to insert some values into one table based on the entries of the other. I want to know if it is possible to accomplish the task using a AWS GLUE job, if so, will that be a good idea? Or should I use the query-editor/sqlworkbench in Redshift to accomplish the task.

  • Table 1 has the following schema:
Person(id,firstName,Lastname)
  • Table 2 has the following schema
Selection(perId,check)
  • If the concatenation of firstName and lastName of Person table lies in ['fullName1', 'fullName2',..] then then insert 1 in the selection table otherwise 0 with the respective id of person.

Example

  • the list values are: ['JohnLuie' , 'FranklinWatson']

    person table

    Id   |        Firstname           |   lastName   
    04   |           John             |           Luie
    09   |           Ben              |         Johnson

Initially the Selection Table is empty. So after checking with the condition on person table That is if

  • (Person.firstName+ Person.lastName) in ['JohnLuie' , 'FranklinWatson] then insert 1 or 0 in Selection.check with person.id in Selection.perId

So after performing the task the Selection table will look like:

Selection

    PerId      |     check
     04        |        1
     09        |        0

I want to know if I can perform the following task by running aws-glue job. Both the tables are in redshift.

Questioner
Dipayan Som
Viewed
30
John Rotenstein 2020-02-09 14:38

You could just do it in an SQL query, something like:

INSERT INTO Selection
(
    SELECT
      Id,
      CASE WHEN firstName || lastName IN ['JohnLuie' , 'FranklinWatson] THEN 1 ELSE 0 END
    FROM person
)