Note that there are some explanatory texts on larger screens.

plurals
  1. POsql join query not working
    text
    copied!<p>I have two tables with data<br> tab1.</p> <pre><code>------------------------------------------------------------ compid | user_id | compdate | description | ------------------------------------------------------------ C0001 | U000001 | 2012-02-29 | desc1 | C0002 | U000002 | 2012-02-29 | desc1 | C0003 | U000001 | 2012-03-01 | desc1 | C0004 | U000003 | 2012-03-01 | desc1 | C0005 | U000001 | 2012-03-02 | desc1 | C0006 | U000008 | 2012-03-02 | desc1 | C0007 | U000212 | 2012-03-02 | desc1 | C0008 | U010222 | 2012-03-02 | desc1 | C0009 | U000091 | 2012-03-02 | desc1 | C0010 | U010222 | 2012-03-02 | desc1 | ------------------------------------------------------------ </code></pre> <p>tab2.</p> <pre><code>------------------------------------------------------------ compid | assigned_to| assignedon | status | ------------------------------------------------------------ C0001 | U000101 | 2012-02-29 | Closed | C0002 | U000101 | 2012-02-29 | Open | C0003 | U000102 | 2012-03-02 | Closed | C0004 | U000102 | 2012-03-02 | Closed | C0005 | U000101 | 2012-03-02 | Open | C0006 | U000101 | 2012-03-02 | Closed | C0008 | U000101 | 2012-03-02 | Closed | ------------------------------------------------------------ </code></pre> <p>Now what I want is:<br> All the records whose <code>Status = 'Open'</code> and also the record from <code>tab1</code> whose entry is not in <code>tab2</code>.<br> The query should fetch records whose <code>compdate = '2012-03-02'</code>.</p> <p>What I tried is: </p> <pre><code>select from tab1 a left join dbo.tab2 b on a.CompId = b.CompId where b.StatusFlag = 'Open' and a.CompDate = CONVERT(nvarchar(30),Dateadd(day,-1,getdate()),106) </code></pre> <p>Expected results :</p> <pre><code>--------------------------------------------------------------------------------------- compid | user_id | compdate |description |assigned_to | assignedon |status| --------------------------------------------------------------------------------------- C0005 | U000001 | 2012-03-02 | desc1 | U000101 | 2012-03-02 | open | C0009 | U000001 | 2012-03-02 | desc1 | Null | NULL | null | C0010 | U000001 | 2012-03-02 | desc1 | null | null | null | ---------------------------------------------------------------------------------------- </code></pre>
 

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