Example data:
Table1 Table2
x | y x | y
----- -----
1 | A 1 | A
1 | B 1 | D
1 | C 2 | M
1 | null 2 | N
2 | M 2 | M
2 | N 1 | A
I want to count number of x's in Table2 and join it with Table1. But in Table2 I can have y's which doesn't match to y's in Table1. In this case I want to join them with row with null in y column in Table1. In an example Table2.1-D should be joined with Tabl1.1-null
For given example I expect the result:
x | y | count
-----------------
1 | A | 2
1 | B | 0
1 | C | 0
1 | null | 1 (because D doesn't match to anything else in Table1)
2 | M | 2
2 | N | 1