Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server : left join results in fewer rows than in left table
    primarykey
    data
    text
    <p>I am using SQL Server (I believe it is 2005).</p> <p>I have <code>TableA</code> that has 2 columns and 439 rows (each row is unique). </p> <pre><code>+----------+ |ID | Name | +----------+ </code></pre> <p>I have <code>TableB</code> that has 35 columns and many hundreds of thousand rows (each row is also unique). </p> <pre><code>+------------------------------------------------------------------------------+ |Date | ID | Name | Blah1 | Blah2 | ... | Hour1 | Hour2 | Hour3 | ... | Hour24 | +------------------------------------------------------------------------------+ </code></pre> <p>Each row in <code>TableB</code> has hourly observations and some other house keeping information. Now for testing purposes I am only interested in today's date i.e 4/19/2013. </p> <p>If I do:</p> <pre><code>Select count(*) from TableB where Date = '4/19/2013 12:00:00 AM' </code></pre> <p>I get 10526, which is correct as there are 10526 distinct locations for which there is hourly observation data each day. </p> <p>I want to LEFT JOIN TableA and TableB <code>on a.id = b.id</code>, which SHOULD produce a result that has 439 rows. </p> <p>Unfortunately, the result has 246 rows. How can this be? Isn't a <code>LEFT JOIN</code> suppose to return all rows in <code>TableA</code> regardless of whether there was a match in <code>TableB</code>? </p> <p>*<strong><em>EDIT</em>*</strong></p> <p>The complete query I used was:</p> <pre><code>select * from TableA as a left join TableB as b on a.id = b.id where RealDate = '4/20/2013 12:00:00 AM' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload