如何在动态数据透视中舍入数字?

时间:2017-08-31 13:57:32

标签: sql-server-2008

所以我有这个:

DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)


--Get distinct values of the PIVOT Column 
SELECT @ColumnName= ISNULL(@ColumnName + ',','') 
       + QUOTENAME(period)
FROM (SELECT DISTINCT period FROM atbv_Accounting_Transactions WHERE lAccountNO LIKE '6%' AND Period LIKE '2017%') AS Periods

SET @DynamicPivotQuery = 
    N'SELECT lAccountNo, ' + @ColumnName + '
    FROM (SELECT
    lAccountNo, period, SUM(Amount) As Total
    FROM atbv_Accounting_Transactions 
    WHERE lAccountNO LIKE ''6%'' AND Period LIKE ''2017%''
    GROUP BY lAccountNo, period
    ) AS T
    PIVOT(SUM(TOTAL) 
          FOR period IN (' + @ColumnName + ')) AS PVTTable'

--Execute the Dynamic Pivot Query
 EXEC sp_executesql @DynamicPivotQuery

它让我回答:

enter image description here

如何删除select本身中的小数位数。我无法编辑表中的列并减少小数位。所以我需要编辑此查询以返回不带小数的值。

谢谢!

1 个答案:

答案 0 :(得分:2)

只需将SUM(Amount)更改为cast(SUM(Amount) as int)floor(SUM(Amount)),它就可以实现。