How to check every record in relative table in sql server
<p>I am struggling with one query.  </p>

<p>I have three tables 
deviations : Can have more than one measures
measure_sources : relation table of deviation and measure
measures</p>

<p>Here I want to check this on every measure related to deviation
due_at > currentdate : should visible
due_at &lt; currentdate : not shown</p>

<p>Deviation Table</p>

<pre><code>id name
1   abc
2   def
</code></pre>

<p>measure sources table</p>

<pre><code>id  element_id measure_id
1      1        1
2      1        2
3      1        3
4      2        1
5      2        2
</code></pre>

<p>measures table</p>

<pre><code>id  due_at
1    2015-10-14
2    2015-12-30
3    2016-11-30
</code></pre>

<p>expected result</p>

<pre><code>id 
2
</code></pre>

<p>Currently I am using below query but I am getting the deviation that should not be come</p>

<pre><code>SELECT  d.id FROM deviations d 
LEFT JOIN measure_sources ms ON ms.element_id = d.id 
LEFT JOIN measures m ON ms.measure_id = m.id 
WHERE (m.due_at &gt; '2016-08-15' OR m.due_at is null)
</code></pre>

<p>But currently I am getting 1 and 2 both as result 
please suggest where I am wrong</p>

<p>Thanks</p>