我在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
提前致谢
答案 0 :(得分: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
答案 1 :(得分: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