I have two tables with data
tab1.
------------------------------------------------------------
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 |
------------------------------------------------------------
tab2.
------------------------------------------------------------
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 |
------------------------------------------------------------
Now what I want is:
All the records whose Status = 'Open' and also the record from tab1 whose entry is not in tab2.
The query should fetch records whose compdate = '2012-03-02'.
What I tried is:
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)
Expected results :
---------------------------------------------------------------------------------------
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 |
----------------------------------------------------------------------------------------
1 Answer