2

我在 SQL Server Management Studio 2005 中运行了一个 SQL 查询,当我的结果来自我的Total_Cost列时,我的列没有按照我想要的格式进行格式化。

我曾尝试添加一个Declare @TOTAL_COST decimal(12,2)子句,但似乎无法使其正常工作,因此再次将其取出。

我的查询是:-

SELECT     
   vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATID AS COST_CAT_CODE, 
   vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATNME AS COST_CAT_NAME, 
   vwREP_GP_ContractProjectPurchaseRpt.COST, 
   vwREP_GP_ContractProjectPurchaseRpt.ITEMDESC AS ITEM, 
   vwREP_GP_ContractProjectPurchaseRpt.CREDITOR, 
   vwREP_GP_ContractProjectPurchaseRpt.DOC_NUM AS DOCUMENT_NUMBER, 
   vwREP_GP_ContractProjectPurchaseRpt.DESCRIPTION, 
   vwREP_GP_ContractProjectPurchaseRpt.DOC_DATE AS DATE,  
   vwREP_GP_ContractProjectPurchaseRpt.PROJECT AS JOB_CODE_STAGE, 
   vwREP_GP_ContractProjectPurchaseRpt.COMPOUNDNAME AS CATEGORY_DESCRIPTION
FROM         
   vwREP_GP_ContractProjectPurchaseCostCatID 
INNER JOIN
   vwREP_GP_ContractProjectPurchaseRpt ON vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATID = vwREP_GP_ContractProjectPurchaseRpt.PACOSTCATID

SELECT  
   CATEGORY_DESCRIPTION, SUM(COST) AS TOTAL_COST 
FROM    
   (SELECT 
        COMPOUNDNAME AS CATEGORY_DESCRIPTION, COST 
    FROM   
        vwREP_GP_ContractProjectPurchaseRpt 
   ) A
GROUP BY 
    CATEGORY_DESCRIPTION 

这给了我我正在寻找的结果,但Total_Cost产生的数字是 5 位十进制格式。

例如:

156713.55000

获得结果以阅读以下内容的最佳方法是什么 -

156713.55

提前致谢

4

2 回答 2

1
SELECT     vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATID AS COST_CAT_CODE, 
           vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATNME AS COST_CAT_NAME, 
           vwREP_GP_ContractProjectPurchaseRpt.COST, 
           vwREP_GP_ContractProjectPurchaseRpt.ITEMDESC AS ITEM, 
           vwREP_GP_ContractProjectPurchaseRpt.CREDITOR, 
           vwREP_GP_ContractProjectPurchaseRpt.DOC_NUM AS DOCUMENT_NUMBER, 
           vwREP_GP_ContractProjectPurchaseRpt.DESCRIPTION, 
           vwREP_GP_ContractProjectPurchaseRpt.DOC_DATE AS DATE,  
           vwREP_GP_ContractProjectPurchaseRpt.PROJECT AS JOB_CODE_STAGE, 
           vwREP_GP_ContractProjectPurchaseRpt.COMPOUNDNAME AS CATEGORY_DESCRIPTION


FROM         vwREP_GP_ContractProjectPurchaseCostCatID INNER JOIN
                      vwREP_GP_ContractProjectPurchaseRpt ON vwREP_GP_ContractProjectPurchaseCostCatID.PACOSTCATID = vwREP_GP_ContractProjectPurchaseRpt.PACOSTCATID



SELECT  CATEGORY_DESCRIPTION, CAST(SUM(COST) AS DECIMAL(12, 2)) AS TOTAL_COST

FROM    (  
        SELECT COMPOUNDNAME AS CATEGORY_DESCRIPTION, COST 
        FROM   vwREP_GP_ContractProjectPurchaseRpt 
        ) A
GROUP BY 
        CATEGORY_DESCRIPTION     
于 2012-05-18T11:46:46.077 回答
0

如果您只存储 2 位小数,那么我不会担心。几乎我所知道的每种语言都会丢弃多余的零(SQL除外):)

另一方面,如果您要存储超过 2 个小数(即 123.456),那么我会使用 round() 来代替准确性。

 ...

SELECT  CATEGORY_DESCRIPTION, round(SUM(COST), 2) AS TOTAL_COST  

FROM    (   
        SELECT COMPOUNDNAME AS CATEGORY_DESCRIPTION, COST  
        FROM   vwREP_GP_ContractProjectPurchaseRpt  
        ) A 

GROUP BY  
        CATEGORY_DESCRIPTION  
于 2012-05-18T11:55:07.127 回答