iRon7 / Join-Object

Combines two objects lists based on a related property between them.
MIT License
107 stars 13 forks source link

Symmetric difference (`OuterJoin`) #30

Closed iRon7 closed 2 years ago

iRon7 commented 2 years ago

Getting everything that is unrelated as e.g.: Windows Powershell: How to compare two CSV files and output the rows that are just in either of the file but not in both appears to have a quite common use. Meaning a FullJoin excluding all the objects in the InnerJoin.

iRon7 commented 2 years ago

Implemented OuterJoin:

$Csv1 = ConvertFrom-Csv @'
name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Alpha,Fan,120,jefferson,Riverside,NJ,8075
'@

$Csv2 = ConvertFrom-Csv @'
name,surname,height,city,county,state,zipCode
John,Doe,120,jefferson,Riverside,NJ,8075
Jack,Yan,220,Phila,Riverside,PA,9119
Jill,Fan,120,jefferson,Riverside,NJ,8075
Steve,Tan,220,Phila,Riverside,PA,9119
Bravo,Tan,220,Phila,Riverside,PA,9119
'@

$Csv1 |OuterJoin $Csv2 |ConvertTo-Csv
name,surname,height,city,county,state,zipCode
Alpha,Fan,120,jefferson,Riverside,NJ,8075
Bravo,Tan,220,Phila,Riverside,PA,9119

More specific:

PS C:\> $dataset1
A B    XY    ZY
- -    --    --
1 val1 foo1  bar1
2 val2 foo2  bar2
3 val3 foo3  bar3
4 val4 foo4  bar4
4 val4 foo4a bar4a
5 val5 foo5  bar5
6 val6 foo6  bar6

PS C:\>  $dataset2
A B    ABC   GH
- -    ---   --
3 val3 foo3  bar3
4 val4 foo4  bar4
5 val5 foo5  bar5
5 val5 foo5a bar5a
6 val6 foo6  bar6
7 val7 foo7  bar7
8 val8 foo8  bar8

PS C:\> $Dataset1 |OuterJoin $Dataset2 -on a,b
A B    XY   ZY   ABC  GH
- -    --   --   ---  --
1 val1 foo1 bar1
2 val2 foo2 bar2
7 val7           foo7 bar7
8 val8           foo8 bar8'

OuterJoin with two value arrays:

1..5 |OuterJoin @(3..7)
1
2
7
8

Note: do not confuse the OuterJoin with the SQL Full Outer Join which is in fact similar to the FullJoin type.