2014-01-07 3 views
0

은 내가 샘플 테이블로 선언 한 날짜에 대한 데이터를 당길 필요하지만 어떻게 든 내가 오류를 얻을 : 하위 쿼리는 1 개 이상의 값을 반환했습니다. 하위 쿼리가 =,! =, <, < =,>,> = 또는 하위 쿼리가 식으로 사용될 때 하위 쿼리가 수행되는 경우에는 허용되지 않습니다.하위 쿼리 반환 하나 이상의 오류가

어떤 일이 있어도이 문제를 막을 수 있고 내 검색어가 날짜 목록에서 데이터를 가져올 수 있습니다.

and First_Start_date between (Select Fixed_Date From @Sample_Alankar) and DATEADD(WEEK,@NumOfDays*(1),(Select Fixed_Date From @Sample_Alankar)) 

을 같은 뭔가 : 여기

는 하위 쿼리로이 작업을 수행하려면

Declare @Sample_Alankar Table 
(
--Author_id int, 
Sent_date Datetime, 
Fixed_Date datetime 
) 
Declare @StartDate DateTime 
Declare @EndDate DateTime 
Declare @NumOfDays Int 

Set @NumOfDays = 4 
Set @StartDate = '5/1/2013' 
Set @EndDate = '12/31/2013' 

insert @Sample_Alankar values 
('11/5/2013','11/13/2013'), 
('11/5/2013','11/13/2013'), 
('11/4/2013','11/13/2013'), 
('9/18/2013','9/25/2013'), 
('11/4/2013','11/13/2013'), 
('9/3/2013','9/10/2013'), 
('11/5/2013','11/6/2013'), 
('11/4/2013','11/12/2013'), 
('11/4/2013','11/12/2013'), 
('11/4/2013','11/12/2013') 

;With Cte_Fixed_Date as 
(Select Distinct (Q.Topic_ID) , First_Start_date , QR.Author_ID 
from Question Q 
Left Join QuestionResponse QR on Q.Topic_ID = QR.Topic_ID 
Left Join exptblFeedback EF with (nolock) on Q.Topic_ID = EF.Topic_ID 
Left Join DWStar.DW03.factFeedback FB with (nolock) on Q.Topic_ID = FB.TopicID 
Join Forum FO with (nolock) on FO.Forum_ID = Q.Forum_ID 
Join exptblCategory C with (nolock) on C.Cat_ID = FO.Cat_ID 
inner join CategoryExpert CE WITH (NOLOCK) on QR.Author_ID = CE.Author_ID and Fo.Forum_ID = CE.Forum_ID and IsReject = 0 
where 
1 = 1 
and First_Start_date between @StartDate and @EndDate 
and Q.Author_ID <> Qr.Author_ID 
and Q.culture in('en-US') 
and C.Cat_name <> 'Fling' 
and First_Start_date between (Select Fixed_Date From @Sample_Alankar) and DATEADD(WEEK,@NumOfDays*(1),(Select Fixed_Date From @Sample_Alankar)) 
group by Q.Topic_ID, First_Start_date, QR.Author_ID 
) 
select * from Cte_Fixed_Date 
+0

이 오류에 관해 많은 비슷한 질문을 했습니까? –

+1

문제는'(고정 된 날짜를 @Sample_Alankar에서 선택하십시오)'입니다. '@ Sample_Alankar'는 여러 행을 가지고 있습니다. 너 뭐하려고? –

+0

@sample_alankar의 각 행마다 매번이를 수행 할 필요없이 선언 된 날짜 범위의 데이터를 한꺼번에 선택하려고합니다. – Alankar

답변

0

는, 다음 교체 할 사전 : 내 쿼리 및 감사입니다

and exists (select 1 
      from @Sample_Alankar sa 
      where First_Start_date between sa.Send_Date and sa.Fixed_Date 
      ) 

그러나 대부분의 사람들은 조인 조건으로 필터링을 수행합니다.

join 
@Sample_Alankar sa 
on First_Start_date between sa.Send_Date and sa.Fixed_Date 
관련 문제