2016-06-30 6 views
0

以下に2つのテーブルがあります。別の選択クエリのwhere句でクエリの戻り値を使用します。

Id  Name 
1A  James 
23  Holly 
33  Rob 

tblData::私は、以下のクエリを書かれているが、それはtick.Nameはマルチパート識別子を言っビットのようではないことができなかったん

Date   Score  Name 
2016-06-01 3.5  James 
2016-06-01 4.5  Holly 
2016-06-01 5.5  Rob 
2016-06-01 2.5  James 
2016-06-01 3.5  Holly 
2016-06-01 6.5  Rob 
... 
2016-06-01 7.5  James 
2016-06-01 11.5  Holly 
2016-06-01 1.5  Rob 

をtblId縛られる。

私は1つのレコードのみが今まで

select Name 
from tblId 
where Id = 33 

クエリによって返されることを知っています。次のselectステートメントのどこの部分でこの値を使用するのが最適な方法ですか?

;with tick as 
(
    select Name from tblId where Id = 33 
) 
select Date, Score 
from tblData 
where Name = tick.Name and Date >= '2016-06-01' 
order by Date 
+2

ティックから選択する必要がありますが、cteは「利用可能」ですが選択されていません。 – jarlh

答えて

2

joinとお考えですか?

with tick as (
    select Name from tblId where Id = 33 
    ) 
select t.Date, t.Score 
from tblData t join 
    tick 
    on t.name = tick.name 
where t.Date >= '2016-06-01' 
order by t.Date 
+0

ああ私はかなり愚かだったよ...長い一日でしたが、ありがとう – mHelpMe

1

あなたはいつもあなたの一つの値だけを与えるそのID、別のテーブルから選択しているので、

select Date, Score from tblData td 
where EXISTS (select 1 from tblId t where t.Id = 33 AND td.Name = t.Name) 
and Date >= '2016-06-01' 
order by Date 
0

あなたはまた、代わりにCTEの変数を使用して検討するかもしれない、これを試してみてください。

1
Select Date, Score from tblData 
where Name in (select Name from tblId where Id = 33) 
    and Date >= '2016-06-01' 
order by Date 
関連する問題