需要在过去12个月的数据中明智地显示Columns季度

时间:2019-06-20 14:31:45

标签: sql sql-server

我要基于当前月份每季度显示最近12个月数据。     例如,当前月份为六月,则我的数据应显示4个分组集JUNE-MAY-APRIL,JUNE-MAY-APRIL,DEC-NOV-OCT,SEPT-AUG-JULY

以下是我要显示的格式 enter image description here

Below is the sql query i am try

    DECLARE @ANYDATE DATETIME
    SET @ANYDATE=GETDATE()

     SELECT TOP 20 DATePART(MONTH,@ANYDATE) [MONTH],
     DATePART(QUARTER,@ANYDATE) [Quarter], COUNT(1) [ACount]
     FROM ACTIVITY ACC
     INNER JOIN Accounts A ON ACC.Relatedtoid = A.ACCOUNTID
        AND ACC.OWNERID = A.OWNERID
     WHERE ACC.layoutid = 1015
        AND ACC.ownerid = 402
        AND A.LAYOUTID IN (
            104442
            ,104451
            ,104436
            )           
       GROUP BY 
      DATePART(MONTH,ACC.CREATEDON),DATePART(QUARTER,ACC.CREATEDON)
      ORDER BY 1,2

OUTPUT OF CURRENT QUERY

enter image description here

0 个答案:

没有答案