performance on different method to merge tables in snowflake

Viewed 638

Table a has id, status;

id status    name
 1  cancel    test

Table b has id, status, metric1, metric2

id status metric1 metric2
 1  home     1      6
 2  home     5      8
 3  home     7      3

I want to form a table c that whenever there's

a inner join a.id=b.id and a.status='cancel',

then table c will have inner join result from a to b, in this example, id 1 in table b match a,I want to output in c the row of b except status change to 'cancel', and insert another record to have metric1, metric2 updated. The final result in table c is:

id status metric1 metric2
 1  cancel     1      6
 1  cancel     0      0

My current query:

select a.id, a.status, b.metric1, b.metric2
from a on a.id = b.id 
and a.status='cancel'
union all
select a.id, a.status, 0, 0
from a on a.id = b.id 
and a.status='cancel'

I am trying to find if there's a better way or even use merge on b instead of creating a table c.

merge into b
  using a on a.id = b.id
  when matched then update 
  set b.status = a.status
  when matched then insert
  (b.id, a.status, 0, 0)

Which method is better of performance? and any other suggestions?

Thanks!

0 Answers
Related