简体   繁体   中英

update using inner query (join)

I think I'm losing it, can anyone explain why am I experiencing the following: Here is my query:

    update financials.operator_summary_daily set
        osm_fos_id = fos_id
    from financials.operator_summary_daily daily
    join (
        select 
            osm_id,
            fos_id
        from financials.operator_summary_daily daily
        join financials.operator_settlement_monthly on daily.osm_opt_id = fos_opt_id and date_trunc('month', daily.osm_timestamp)::timestamp = date_trunc('month', fos_timestamp)::timestamp --and daily.osm_fos_id is null
    ) as result on result.osm_id = daily.osm_id

inner query returns 1963 records, but the update is executed on the entire table of financials.operator_summary_daily and updates all 39K records.

What am I missing here?

Tables:

CREATE TABLE financials.operator_summary_daily
(
  osm_id bigint NOT NULL DEFAULT nextval('financials.operator_summary_monthly_osm_id_seq'::regclass),
  osm_timestamp timestamp without time zone NOT NULL,
  osm_opt_id bigint NOT NULL,
  osm_gam_id bigint NOT NULL,
  osm_cur_id bigint NOT NULL,
  osm_ctt_id bigint NOT NULL,
  osm_turnover double precision NOT NULL,
  osm_revenue double precision NOT NULL,
  osm_timestamp_created timestamp without time zone NOT NULL DEFAULT now(),
  osm_timestamp_updated timestamp without time zone NOT NULL DEFAULT ('now'::text)::date,
  osm_fos_id bigint
);


CREATE TABLE financials.operator_settlement_monthly
(
  fos_id bigserial NOT NULL,
  fos_timestamp timestamp without time zone NOT NULL, -- Monthly timestamp
  fos_opt_id bigint NOT NULL,
  fos_royalties double precision NOT NULL,
  fos_carry_over double precision NOT NULL,
  fos_other_adjustments double precision NOT NULL,
  fos_due double precision NOT NULL,
  fos_collectable double precision NOT NULL,
  fos_balance double precision NOT NULL,
  fos_collected double precision NOT NULL,
  fos_baddebt double precision NOT NULL,
  fos_carryforward double precision NOT NULL,
  fos_ses_id bigint NOT NULL,
  fos_timestamp_created timestamp without time zone NOT NULL DEFAULT now(),
  fos_datetime_updated timestamp without time zone NOT NULL DEFAULT now(),
  fos_prq_id bigint
);

EDIT:
Thank you for your quick answers, however there shouldn't be a need for a where, the join condition is not LEFT, it's inclusive and should render only the matched rows for the update. Therefore, only 1963 records out of the 37K (those meeting the join criteria) should be updated. Am I wrong?

ANSWER: The correct script:

    UPDATE financials.operator_summary_daily d
    SET    osm_fos_id = m.fos_id
    FROM   financials.operator_settlement_monthly m
    WHERE  
        m.fos_opt_id = d.osm_opt_id
        AND    date_trunc('month', d.osm_timestamp) = date_trunc('month', m.fos_timestamp)
        AND d.osm_fos_id is null; 

Thank you @ErwinBrandstetter. Apparently, I've been using a wrong assumption for the JOIN, my background is MS-SQL and it works differently there. From looking at the end end solution, the JOIN was completely dropped and exchanged with a direct call to the secondary table, something you cannot do in T-SQL.

BTW, the following does not work and it does contain a where clause as other comments suggested. I'm still puzzled as to how pgsql works with regards to JOIN when Updating records.

    update financials.operator_summary_daily set
        osm_fos_id = fos_id
    from financials.operator_summary_daily daily
    join (
        select 
            osm_id,
            fos_id
        from financials.operator_summary_daily daily
        join financials.operator_settlement_monthly on daily.osm_opt_id = fos_opt_id and date_trunc('month', daily.osm_timestamp)::timestamp = date_trunc('month', fos_timestamp)::timestamp 
    ) as result on result.osm_id = daily.osm_id
    where
        daily.osm_id = result.osm_id
        and daily.osm_fos_id is null;
  • The complicated join is a result of me trying to ensure the inner query returns only a subset of the whole table for "debugging" purposes.

Thanks again!

There is no WHERE condition to your UPDATE.

This statement will update all rows.

Your statement might work like this (much simplified):

UPDATE financials.operator_summary_daily d
SET    osm_fos_id = m.fos_id
FROM   financials.operator_settlement_monthly m
WHERE  m.fos_opt_id = d.osm_opt_id
AND    date_trunc('month', d.osm_timestamp) = date_trunc('month', m.fos_timestamp)
-- AND daily.osm_fos_id is null -- ?? Why is this here / commented out?

You don't have a WHERE clause linking to table to be updated to the result the query after FROM

You need to do something like

update financials.operator_summary_daily set
    osm_fos_id = fos_id
from financials.operator_summary_daily daily
join (
    select 
        osm_id,
        fos_id
    from financials.operator_summary_daily daily
    join financials.operator_settlement_monthly on daily.osm_opt_id = fos_opt_id and date_trunc('month', daily.osm_timestamp)::timestamp = date_trunc('month', fos_timestamp)::timestamp --and daily.osm_fos_id is null
) as result on result.osm_id = daily.osm_id
WHERE daily.osm_id = financials.operator_summary_daily.osm_id -- condition to link

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