子查询返回多个错误

时间:2014-01-07 00:28:44

标签: sql-server date datetime subquery

我需要为示例表中已声明的日期提取数据,但不知何故我收到错误: 子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

有什么方法可以阻止Happening这个,并允许我的查询从日期列表中提取数据。

以下是我的询问并提前致谢:)

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

1 个答案:

答案 0 :(得分:0)

如果要将其作为子查询执行,则需要替换:

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

有这样的事情:

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