Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
284 views
in Technique[技术] by (71.8m points)

SQL (Bigquery) - Populate multiple columns in existing rows

EDIT: As @Prateek suggested, I changed my working flow - Now the first query is used as "Structure-Query" that I run every time that I want to clear and recreate my table with the desired variables structure. The second query became my "Population-Query", where I fill my empty structured table. The meaning is that now I have a bunch of sub-tables that my population-query joins and push at once.

Given this structured table with no rows:

CREATE OR REPLACE primary_table (
        EVENT_ID int64,
        TREATMENT_TIME timestamp,
        AGE int64,
        HEIGHT int64,
        WEIGHT int64,
        SEX string, 
        ICU_ADMIT timestamp,
        ICU_DISCH timestamp,
        ICUSTAYS_COUNT int64,
);

And after the initial population with the total wanted rows at the end of the day:

INSERT INTO primary_Table (
        EVENT_ID,
        TREATMENT_TIME
    )
   SELECT 
        event_id,
        cast(treatment_time as timestamp)
   FROM some_table_1

I have this half filled primary_table:

EVENT_ID TREATMENT_TIME AGE HEIGHT WEIGHT SEX ICU_ADMIT ICU_DISCH ICUSTAYS_COUNT
32342 2156-03-30 06:41:00 UTC
32342 2156-03-30 06:41:00 UTC
45084 2165-03-29 10:23:00 UTC
45084 2165-03-29 10:23:00 UTC
question from:https://stackoverflow.com/questions/65644127/sql-bigquery-populate-multiple-columns-in-existing-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Multiple rows can be inserted, but updating existing rows would be difficult in a single shot. As for updating specific values to each row, you need to add a unique WHERE clause.

A smarter solution would be to write a small piece of code, that would read the data from the source (file/network, etc) and perform a bulk update on the primary_table

Or if you are trying to copy from another table, write a Stored Function to read data from another table and based on EVENT_ID, construct a query to update primary_table


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...