2012-03-03 75 views
-1

我有兩個數據表
tab1。sql加入查詢不起作用

------------------------------------------------------------ 
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 | 
---------------------------------------------------------------------------------------- 
+0

如果你想記錄狀態'打開'爲什麼你使用'!='? – bernie 2012-03-03 05:51:02

+1

那麼問題是什麼?你是否得到一個錯誤或只是不是預期的結果? – 2012-03-03 05:51:54

+0

看起來您正在選擇compdate在當前日期前兩天的行。如果你想要compdate是'2012-03-02'的行,你必須等到週日才能運行它。 – 2012-03-03 05:55:23

回答

3
Select * 
From 
    Tab1 
Left Join 
    Tab2 
on 
    Tab1.CompID = Tab2.CompID 
Where 
    (Tab2.Status = 'open' or 
    Tab2.Status is null) and 
    Tab1.CompDate = '2012-03-02' 
0

這是你想選擇什麼呢?

select * from tab2 t2 
left join tab1 t1 on t1.compid = t2.compid 
where t2.[status] = 'Open' and 
t1.compdate in (select compdate from tab1 except 
select assignedon from tab2) 
and t1.compdate = '20120302' 
0

我明白你正在尋找的數據的聯合:與狀態的所有記錄是從TAB1打開union記錄不存在TAB2。這將是該查詢:

select * from (
    select t1.compid, t1.user_id, t1.compdate from tab1 t1 
    left join tab2 t2 on t1.compid = t2.compid 
    where t2.compid is null 
    union 
    select compid, assigned_to, assignedon from tab2 
    where statusflag = 'Open' 
) t 
where compdate = '2012-03-02' 
+0

沒有工會是必要的。請參閱Aheho的正確答案。 – 2012-03-03 07:54:34