У меня есть две таблицы с данными
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 |
------------------------------------------------------------
Теперь я хочу:
Все записи, чья Status = 'Open'
, а также запись из tab1
, чья запись не в tab2
.
Запрос должен извлекать записи, чьи compdate = '2012-03-02'
.
То, что я пробовал, это:
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)
Ожидаемые результаты:
---------------------------------------------------------------------------------------
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 |
----------------------------------------------------------------------------------------