如何获取上一年的当前月份和前几个月

时间:2020-02-06 04:45:25

标签: sql sql-server tsql

我要求获取一年中的当前月份和去年的月份。像这样: enter image description here

现在我所做的是像这样硬编码:

, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 1 THEN trans.OutQty END), 0.00) as 'Jan'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 2 THEN trans.OutQty END), 0.00) as 'Feb'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 3 THEN trans.OutQty END), 0.00) as 'Mar'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 4 THEN trans.OutQty END), 0.00) as 'Apr'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 5 THEN trans.OutQty END), 0.00) as 'May'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 6 THEN trans.OutQty END), 0.00) as 'Jun'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 7 THEN trans.OutQty END), 0.00) as 'Jul'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 8 THEN trans.OutQty END), 0.00) as 'Aug'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 9 THEN trans.OutQty END), 0.00) as 'Sep'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 10 THEN trans.OutQty END), 0.00) as 'Oct'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 11 THEN trans.OutQty END), 0.00) as 'Nov'
, ISNULL(SUM(CASE WHEN month(trans.DocDate) = 12 THEN trans.OutQty END), 0.00) as 'Dec'

是否可以动态地执行而不是进行硬编码?

2 个答案:

答案 0 :(得分:1)

请根据您的要求在MSSQL服务器上找到以下答案。

 DECLARE @v_date DATETIME =GETDATE()    --'2019-11-11'

;WITH months(MonthNumber) AS
(
    SELECT 0
    UNION ALL
    SELECT MonthNumber+1 
    FROM months
    WHERE MonthNumber < 13
)
SELECT  
        LEFT(DATENAME(MONTH,DATEADD(MONTH,-MonthNumber,@v_date)),3)+'-'+LEFT(DATENAME(YEAR,DATEADD(MONTH,-MonthNumber,@v_date)),10) AS [Month],
        MonthNumber+1 AS Month_number ,
        CASE WHEN CONCAT('Current Month - ' ,MonthNumber) = 'Current Month - 0' THEN 'Current Month' ELSE CONCAT('Current Month - ' ,MonthNumber) END AS Remark
FROM months;

enter image description here

答案 1 :(得分:0)

这是动态查询

     declare @monthstart int=1,@monthend int=12,@strquery varchar(max)=''
     while(@monthstart<=@monthend)
     begin
        SET @strquery=@strquery+', ISNULL(SUM(CASE WHEN month(trans.DocDate) =' +Convert(varchar,@monthstart)+ ' THEN trans.OutQty END), 0.00) as '+SUBSTRING(DateName( month , DateAdd( month , @monthstart , -1 ) ),1,3)+''
        SET @monthstart=@monthstart+1
     end
     print(@strquery)

下面是您需要的输出

, ISNULL(SUM(CASE WHEN month(trans.DocDate) =1 THEN trans.OutQty END), 0.00) as Jan, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =2 THEN trans.OutQty END), 0.00) as Feb, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =3 THEN trans.OutQty END), 0.00) as Mar, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =4 THEN trans.OutQty END), 0.00) as Apr, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =5 THEN trans.OutQty END), 0.00) as May, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =6 THEN trans.OutQty END), 0.00) as Jun, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =7 THEN trans.OutQty END), 0.00) as Jul, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =8 THEN trans.OutQty END), 0.00) as Aug, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =9 THEN trans.OutQty END), 0.00) as Sep, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =10 THEN trans.OutQty END), 0.00) as Oct, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =11 THEN trans.OutQty END), 0.00) as Nov, 
ISNULL(SUM(CASE WHEN month(trans.DocDate) =12 THEN trans.OutQty END), 0.00) as Dec