Subject Re: [firebird-support] Merge in Fb3 fails
Author Dimitry Sibiryakov
14.05.2016 22:04, kyleblm@... [firebird-support] wrote:
> Above works. Merge below fails:
>
> merge into ZipCodeRef T1
> using ZipCodeDataBus T2
> on T2.ZipCode = T1.ZipCode
> when not matched then
> insert (ZipCode, Longitude, Latitude) values (T2.ZipCode, T2.Longitude, T2.Latitude);

By SQL standard MERGE must fail if several records from source matches single record in
target. Add grouping to your source query like this:

merge into ZipCodeRef T1
using
(select ZipCode, MAX(Longitude) lo, MAX(Latitude) la
from ZipCodeDataBus
group by 1) as T2
on T2.ZipCode = T1.ZipCode
when not matched then
insert (ZipCode, Longitude, Latitude) values (T2.ZipCode, T2.Lo, T2.La);

--
WBR, SD.