Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL Server中查找两个季度之间的记录?_Sql_Sql Server - Fatal编程技术网

在SQL Server中查找两个季度之间的记录?

在SQL Server中查找两个季度之间的记录?,sql,sql-server,Sql,Sql Server,我想在SQL Server中查找不同年份的两个季度之间的记录 SELECT ('Q'+cast(DATEPART(QUARTER,calldate) as varchar(3))+'-'+cast(YEAR(calldate) as varchar(4))) period,providerid,volume as volume,type FROM table V where DATEPART(QUARTER,calldate) between @Q1 and @Q2 and

我想在SQL Server中查找不同年份的两个季度之间的记录

SELECT    ('Q'+cast(DATEPART(QUARTER,calldate) as varchar(3))+'-'+cast(YEAR(calldate) as varchar(4)))
period,providerid,volume as volume,type  
FROM  table V    
where DATEPART(QUARTER,calldate) between @Q1 and @Q2 and                             
datepart(year,calldate) in (@Y1,@Y2)  and providerid=@carrierID  
这里Q1=4和Q2=3以及@Y1=2014和@Y2=2016

 Now,I have only records from Q2-2016, So it should return available records      
 but I am getting blank rows.
 if I change the parameter like this 
 Here Q1=3 and Q2=4 and @Y1=2014,@Y2=2016 then i am getting records.
我想要这两个季度之间的所有记录,比如(2014年第3季度和2016年第2季度)。

这里有一种方法:

where datename(year, calldate) + datename(quarter, calldate)
          between @Y1 + @Q1 and @Y2 + @Q2;
这假设变量实际上是字符串。您也可以使用数字来执行此操作:

where datepart(year, calldate) * 10 + datename(quarter, calldate)
          between @Y1 * 10 + @Q1 and @Y2 * 10 + @Q2;
下面是一种使用索引的方法:

where calldate >= datefromparts(@Y1, @Q1*3 - 2, 1) and
      calldate < (case when @Q2 = 4
                       then datefromparts(@Y2 + 1, 1, 1)
                       else datefromparts(@Y2, @Q2*3 - 2 + 3, 1)
                  end)
其中calldate>=datefromparts(@Y1,@Q1*3-2,1)和
calldate<(当@Q2=4时)
然后是datefromparts(@Y2+1,1,1)
else datefromparts(@Y2,@Q2*3-2+3,1)
(完)
这里有一种方法:

where datename(year, calldate) + datename(quarter, calldate)
          between @Y1 + @Q1 and @Y2 + @Q2;
这假设变量实际上是字符串。您也可以使用数字来执行此操作:

where datepart(year, calldate) * 10 + datename(quarter, calldate)
          between @Y1 * 10 + @Q1 and @Y2 * 10 + @Q2;
下面是一种使用索引的方法:

where calldate >= datefromparts(@Y1, @Q1*3 - 2, 1) and
      calldate < (case when @Q2 = 4
                       then datefromparts(@Y2 + 1, 1, 1)
                       else datefromparts(@Y2, @Q2*3 - 2 + 3, 1)
                  end)
其中calldate>=datefromparts(@Y1,@Q1*3-2,1)和
calldate<(当@Q2=4时)
然后是datefromparts(@Y2+1,1,1)
else datefromparts(@Y2,@Q2*3-2+3,1)
(完)

可以尝试这样的方法

SELECT    
 ('Q'+cast(DATEPART(QUARTER,calldate) as varchar(3))+'-'+cast(YEAR(calldate) as varchar(4)))
period,providerid,volume as volume,type  
FROM  table V    
where DATEPART(QUARTER,calldate) + datepart(year,calldate) *4  between @Q1 + @Y1 * 4  and @Q2 + @Y2 * 4 

我可以试试这样的

SELECT    
 ('Q'+cast(DATEPART(QUARTER,calldate) as varchar(3))+'-'+cast(YEAR(calldate) as varchar(4)))
period,providerid,volume as volume,type  
FROM  table V    
where DATEPART(QUARTER,calldate) + datepart(year,calldate) *4  between @Q1 + @Y1 * 4  and @Q2 + @Y2 * 4 

年度和季度是tinyint@Ramdeoangh . . . 如果它们是
tinyint
,你就有问题了,因为它不够大,无法存储一年()。你的@year是否完整,例如2016年,但不只是16年?@Gordon Linoff:我刚刚将它转换为varchar,它就可以工作了。谢谢,年度和季度是tinyint@Ramdeoangh . . . 如果它们是
tinyint
,你就有问题了,因为它不够大,无法存储一年()。你的@year是否完整,例如2016年,但不只是16年?@Gordon Linoff:我刚刚将它转换为varchar,它就可以工作了。谢谢你,伙计