0

次の MySQL 更新を試みましたが、最初のフィールドのみが設定されc.avg_price_dil、2 番目のフィールドは設定されませんc.avg_dilution

奇妙な部分は、以下に示すように更新を2つの部分に分割すると、両方のフィールドが更新されて両方が機能することです。この呼び出しに影響を与えるグループベースの関数について何かありますか?

2 つのフィールドが更新されました- のみc.avg_price_dilが更新されます。

update corp_act as c
inner join (
  select 
      m.gvkey, m.gvkey_iid, m.date_base, avg(m.price_dil) as avg_price, 
      avg(m.dilution) as avg_dilution
  from master as m
  inner join corp_act as c
  on c.gvkey = m.gvkey AND
      m.date_base between c.date_base AND 
                          LAST_DAY(c.date_base + INTERVAL 11 MONTH)
  where c.actioncd not in ('N/C','REA','SPN') AND 
        c.actioncd IS NOT NULL
  group by m.gvkey_iid, c.actioncd) as x
on 
   x.gvkey = c.gvkey AND 
   x.date_base = c.date_base
set 
    c.avg_price_dil = if(x.avg_price is null, 0, x.avg_price) AND
    c.avg_dilution = if(x.avg_dilution is null, 0, x.avg_dilution)

2 つの更新に分割すると、両方とも機能します。

希釈の更新: - 正常に動作します

update corp_act as c
inner join (
  select 
       m.gvkey, m.gvkey_iid, m.date_base, avg(m.dilution) as avg_dilution
  from master as m
  inner join corp_act as c
  on c.gvkey = m.gvkey AND
     m.date_base between c.date_base AND 
                         LAST_DAY(c.date_base + INTERVAL 11 MONTH)
  where c.actioncd not in ('N/C','REA','SPN') AND 
        c.actioncd IS NOT NULL
  group by m.gvkey_iid, c.actioncd) as x
on 
   x.gvkey = c.gvkey AND 
   x.date_base = c.date_base
set 
   c.avg_dilution = if(x.avg_dilution is null, 0, x.avg_dilution)

価格の更新: - 正常に動作します

update corp_act as c
inner join (
  select 
     m.gvkey, m.gvkey_iid, m.date_base, avg(m.price_dil) as avg_price
  from master as m
  inner join corp_act as c
  on c.gvkey = m.gvkey AND
     m.date_base between c.date_base AND 
                         LAST_DAY(c.date_base + INTERVAL 11 MONTH)
  where c.actioncd not in ('N/C','REA','SPN') AND 
        c.actioncd IS NOT NULL
  group by m.gvkey_iid, c.actioncd) as x
on 
   x.gvkey = c.gvkey AND 
   x.date_base = c.date_base
set 
   c.avg_price_dil = if(x.avg_price is null, 0, x.avg_price)

これら 2 つの唯一の違いは、avg関数を個々の関数で 1 回しか使用しないことです。

何か案は?ご協力いただきありがとうございます。

4

1 に答える 1

3

set c.avg_price_dil = if(x.avg_price is null, 0, x.avg_price) AND c.avg_dilution = if(x.avg_dilution is null, 0, x.avg_dilution)

ANDコンマ ( )に置き換えてみてください,

http://dev.mysql.com/doc/refman/5.0/en/update.html

于 2012-06-28T07:58:46.857 に答える