-1

Table 1:

zipCode    Brand 
11111      Toyota

Table 2:

zipCode   Dealers
11111     SouthDealer
22222     NorthDealer
33333     EastDealer

If I do a left join on zipCode then I get

zipCode    Brand    Dealers
11111      Toyota   SouthDealer
22222      BMW      NULL
33333      Honda    NULL

How to fill Null values?

zipCode    Brand    Dealers
11111      Toyota   SouthDealer
22222      BMW      SouthDealer
33333      Honda    SouthDealer

What if Table 1 contained multiple rows like below and I wanted to get the same result.

Table 1:

zipCode    Brand   
11111      Toyota
22222      BMW      
33333      Honda    

Expect result to be something like this:

zipCode    Brand    Dealers
11111      Toyota   SouthDealer
22222      BMW      SouthDealer
33333      Honda    SouthDealer
11111      Toyota   NorthDealer
22222      BMW      NorthDealer
33333      Honda    NorthDealer
11111      Toyota   EastDealer
22222      BMW      EastDealer
33333      Honda    EastDealer
GMB
  • 216,147
  • 25
  • 84
  • 135
tmd13
  • 55
  • 2
  • 6
  • Where does BMW and Honda come from, none are in your first two tables. – Stu Dec 30 '22 at 13:36
  • Why would you fill those nulls with `SouthDealer`? Are you **inventing** data when it's not available? – The Impaler Dec 30 '22 at 14:05
  • Not necessarily inventing data. It is just a way to list what brands and zip codes each dealer can potentially be. – tmd13 Dec 30 '22 at 14:12

1 Answers1

0

From the comments:

list what brands and zip codes each dealer can potentially be

It looks like you want all possible combinations between the two tables. If so, you can just cross join them, to generate their Cartesian product:

select t1.zipcode, t1.brand, t2.dealer
from table1 t1
cross join table2 t2

You might also want to check if each combination actually exists (ie if the dealer exists at the zip code of the brand); here is one way to do it with exists and a case expression:

select t1.zipcode, t1.brand, t2.dealer,
    case when exists(select 1 from table2 t20 where t20.dealer = t2.dealer and t20.zipcode = t1.zipcode) 
        then 1 
        else 0 
    end dealer_exists
from table1 t1
cross join table2 t2
GMB
  • 216,147
  • 25
  • 84
  • 135