SQL查询以年份的最后2位数字获取最后4个季度

时间:2019-02-20 09:47:21

标签: sql-server sql-server-2008 sql-server-2012 sql-server-2008-r2

我需要从表中获取最后4条记录,并且它的记录具有列值,例如'19Q1','18Q4'。

使用下面的查询,我可以获得最近的四个季度,但是该查询有没有最简单的形式。

SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,0,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,0,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period
SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,-1,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,-1,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period
SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,-2,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,-2,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period
SELECT CAST(DATEPART(QUARTER,  CONVERT(DATE, DATEADD(q, DATEDIFF(q,0,DATEADD(q,-3,GETDATE())), 0) )) as VARCHAR) + 'Q' + CAST(Right(Cast(Year(DATEADD(q, DATEDIFF(q,0,DATEADD(q,-3,GETDATE())), 0) ) As varchar(4)),2) as VARCHAR) as Period

O / P:

1Q19
4Q18
3Q18
2Q18

3 个答案:

答案 0 :(得分:4)

假设您正在使用2012(因为您已经标记了3种不同版本的SQL Server ...)如何操作:

SELECT CONCAT(DATEPART(QUARTER,Q.D),'Q',RIGHT(DATEPART(YEAR,Q.D),2))
FROM (VALUES(0),(-1),(-2),(-3)) V(i)
     CROSS APPLY (VALUES(DATEADD(QUARTER,V.i,GETDATE()))) Q(D);

答案 1 :(得分:1)

SELECT CAST( DATEPART(q, DATEADD(q, -v.num, d.CurrentDate) ) AS VARCHAR(1)) 
+ 'Q'
+ RIGHT( CAST( DATEPART(yyyy, DATEADD(q, -v.num, d.CurrentDate)) AS VARCHAR(4) ), 2 )
FROM ( VALUES ( GETDATE() ) ) d(CurrentDate)
    CROSS JOIN ( VALUES (0), (1), (2), (3) ) v(num)
ORDER BY DATEADD(q, -v.num, d.CurrentDate) DESC

答案 2 :(得分:0)

获取当前季度的开始日期,该日期可以追溯到5个季度加上Qx(格式年份)

--QCurrent
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0) as QCurrentStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 0, 0))) as nvarchar(6)) as QLast

--LastQuarter
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0) as LastQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 1, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) + 1, 0))) as nvarchar(6)) as QCurrent

--SecondQuarterBack
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0) as SecondQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 2, 0))) as nvarchar(6)) as SecondCurrent

--ThreeQuarterBack
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0) as SecondQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 3, 0))) as nvarchar(6)) as SecondCurrent

--FourQuarterBack
select DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 4, 0) as SecondQuarterStartDate, 
'Q' + cast(datepart(quarter,( DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) - 4, 0))) as nvarchar(6)) + ' - ' 
+ cast(datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) -4, 0))) as nvarchar(6)) as SecondCurrent

select datepart(year, (DATEADD(qq, DATEDIFF(qq, 0, GETDATE()) -4, 0)))

select format(getdate(), 'yy')