title: SQL Group by using different conditions and multiple columns
body: <p>I have an Addresses Table, with columns being: <strong>A,B,C,D......Address, Number, State, City, Zip</strong>**</p>
  <p>I need to get a count of distinct addresses that can be grouped by 2 different conditions:</p>
  <p>(Address, Number, State, City)
  OR
  (Address, Number, Zip)</p>
  <p>The reason for the 2 conditions is that Zip can be null in some cases with City and State provided and vice versa.
  <strong>However, in cases where all 3 are present, the final table should just have 1 row for that address</strong></p>
  <pre><code>select State, City, Address, Number
  into T1
  from Main Table
  group by State, City, Address, Number
  
  select Zip, Address, Number
  into T2
  from Main Table
  group by Zip, Address, Number
  
  select a.*, b.Zip into JoinT1T2 from T1 a inner join T2 b 
  on a.Address=b.Address AND a.Number=b.Number 
  
  select count(*) from JoinT1T2
  </code></pre>
  <p>But this is not giving me the desired result.</p>