在sql中使用IN运算符修改where子句未给出正确的结果

时间:2015-01-28 17:57:21

标签: sql-server

我想从我的查询中获得每种验证类型的问题数量。当我把月份“1月”放在where条件中时,它给出了正确的输出但是当我修改where子句并使用IN运算符时,它没有给出正确的输出。我只想获得去年所有月份的结果集。

查询条件,

select COUNT(Question_ID) Total_Questions, [Validation Type], CreatedMonth from 
(select *, ROW_NUMBER() OVER (PARTITION BY Question_ID ORDER BY Question_ID) AS RowNum 
from 
(select [Validation Type], Question_ID, CreatedMonth from #temp qh (nolock) 
RIGHT JOIN (select distinct Question_ID, CreatedMonth from tbl_Questions q (nolock)  
where CreatedYear = 2014 and CreatedMonth = '01_Jan'
--and CreatedMonth in ('01_Jan','02_Feb','03_Mar','04_Apr','05_May','06_Jun'
--,'07_Jul','08_Aug','09_Sep','10_Oct','11_Nov','12_Dec')
) t on qh.question = t.Question_ID) as a) as tmp
where tmp.RowNum = 1
group by [Validation Type], CreatedMonth
order by 3,1

我得到以下结果是正确的,

Total_Questions    Validation Type    CreatedMonth
--------------------------------------------------
      236              Inquiry           01_Jan
      326              General           01_Jan
      433              Unknown           01_Jan
      556              Firm              01_Jan

但是当我按照下面更改where子句时,

where CreatedYear = 2014 --and CreatedMonth = '01_Jan'
and CreatedMonth in ('01_Jan','02_Feb','03_Mar','04_Apr','05_May','06_Jun','07_Jul','08_Aug','09_Sep','10_Oct','11_Nov','12_Dec')

它给我的结果不正确,如下,

Total_Questions    Validation Type    CreatedMonth
--------------------------------------------------
       79              Inquiry           01_Jan
      197              General           01_Jan
      228              Unknown           01_Jan
      245              Firm              01_Jan

我不明白为什么会这样。

任何想法都会有所帮助和赞赏。

感谢。

0 个答案:

没有答案