Reputation: 1409
I have tried the following MySQL update but it only sets the first field c.avg_price_dil
and not the second field c.avg_dilution
.
The strange part is that when I split the update into two parts as shown below, both work with both fields being updated. Is there something about group based functions that is affecting this call?
Two fields updated - only c.avg_price_dil
is updated.
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)
When separated into two updates, both work:
Dilution update: - works fine
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)
Price Update: - works fine
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)
The only difference between these two is that I only use the avg
function once in the individual ones.
Any ideas? Thanks for your help.
Upvotes: 0
Views: 83
Reputation: 2121
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)
Try replacing AND
with a comma (,
).
http://dev.mysql.com/doc/refman/5.0/en/update.html
Upvotes: 3