简体   繁体   中英

Insert into where date column value doesn't already exist

I have the below query that I want to create a table new_table and then each day INSERT INTO new_table that checks if the date exists in date_final, if it doesn't load those rows.

The first COALESCE line has 3 values ( p_date, c_date, t_date ), each was created from a CTE from different tables that have a date loaded daily.

INSERT INTO my_table(
    SELECT 
        COALESCE (p_date, c_date, t_date) AS date_final,
        COALESCE (p_name,cl_name,c_name) AS brand
    FROM p  
    FULL JOIN clicks ON cli.c_date = PAGES.p_date
    FULL JOIN comm_agg ON comm_agg.t_date = p.p_date 
    LEFT JOIN ga ON ga.date_f = p.p_date 
)
where date_final dates do not already exist in the new_table

meaning in the new_table if date_final already has the dates 2/12, 2/14 then I want to make sure when doing the insert I want to make sure I don't re-load those dates

INSERT INTO my_table
SELECT 
    COALESCE (p_date, c_date, t_date) AS date_final,
    COALESCE (p_name,cl_name,c_name) AS brand
FROM p  
    FULL JOIN clicks ON cli.c_date = PAGES.p_date
    FULL JOIN comm_agg ON comm_agg.t_date = p.p_date 
    LEFT JOIN ga ON ga.date_f = p.p_date 
where COALESCE (p_date, c_date, t_date) is null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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