现在我所做的是像这样硬编码:
, 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'
是否可以动态地执行而不是进行硬编码?
答案 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;
答案 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