I have two tables as x and y and I want to identify the rows that are identical in both. For each matching row, I need to modify the value of one specific column, then display a combined table with only distinct rows, reflecting the changes. I can achieve this using nested loops, but I’m curious in knowing whether this can be done more efficiently using script modules.
Tables :
let x = [{id:1, name:‘db’},
{id:2, name:‘vsd’},
{id:4, name:‘Henson’}]
let y = [{id:4, name:‘Henson’},
{id:5, name:‘vsrb’}]
Expected Results:
let z = [{id:1, name:‘db’},
{id:2, name:‘vsd’}, {id:4, name:‘UpdatedName’},
{id:5, name:‘vsrb’}]
I can think of a couple of ways. In this example, I’ve renamed the tables xs and ys (because i find it easier to think about it when the names are plural).
let xs = [
{id:1, name:'db'},
{id:2, name:'vsd'},
{id:4, name:'Henson'}
]
let ys = [
{id:4, name:'Henson'},
{id:5, name:'vsrb'}
]
// METHOD 1: Select distinct
return select distinct
r.id,
(case when r in xs and r in ys then "UpdatedName" else r.name end) as name
from (xs & ys) as r
// METHOD 2: Breaking it down into set-like operations
// Calculate the intersection of x and y
let intersection = xs as x where x in ys
// Calculate xs - ys and ys - xs (complement)
let xsMinusYs = xs as x where not (x in ys)
let ysMinusXs = ys as y where not (y in xs)
return xsMinusYs & ysMinusXs & (intersection with { name: "UpdateName" })