2012-03-03 14 views
-1

私はデータを持つ2つのテーブルを持っています
tab1。sql join query not working

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

+1

だから何が問題なの?エラーが発生しているか、予期した結果だけではありませんか? –

+0

現在の日付より2日前の日付の行を選択しているようです。 compdateが '2012-03-02'の行が必要な場合は、これを実行するために日曜日まで待つ必要があります。 –

答えて

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

私はあなたのデータの和集合を探しているの理解:ステータスを持つすべてのレコードは、TAB2に存在していないTAB1からunionレコードをオープンしています。これはクエリーになります:

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の正解を参照してください。 –