在SQL查询中将列设置为第100个dec

时间:2014-09-04 18:20:13

标签: mysql sql decimal

我在这里有一个方差报告查询我需要' Variance'在方差列中没有10个小数点。将方差结果舍入到第100个最方便的方法是什么?

WITH A AS
(
select
A.FACTORY,
A.JOB_NUMBER,
A.PROCESS_STAGE,
A.PART_CODE,
B.PART_DESC_1,
A.INPUT_QTY_STD,
A.QUANTITY_INPUT,
A.QUANTITY_OUTSTANDING,
A.INPUT_QTY_ACTUAL,
(A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)) as variance,
A.ACTUAL_CLOSE_DATE

from 
(select * from [man_prod].[dbo].[JOB_STAGE_LINES]
 where JOB_NUMBER in (select JOB_NUMBER from JOB_OUTPUTS where 
 BF_QTY_ACTUAL<>0 
 and ABS(DATEDIFF(HOUR,ACTUAL_CLOSE_DATE,GETDATE())) < 12 and  STATUS_FLAG='C'
 )) A 
 join fin_prod.dbo.PRODUCT_MASTER B 
 ON A.PART_CODE=B.PART_CODE
 WHERE 
 A.INPUT_QTY_STD<>0 and 
 A.QUANTITY_OUTSTANDING <>0
)

 SELECT * FROM A WHERE A.variance >10.000000 OR A.variance <-10
 order by PROCESS_STAGE asc ,PART_CODE asc, variance desc ;

Variance列出现在00.0000000000我需要它显示00.000或00.000000

非常感谢帮助

3 个答案:

答案 0 :(得分:0)

使用MySQL ROUND()函数,第二个参数是小数位数,如果它是正数。

ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,

在此示例中,如果值为0.0000000000,则会四舍五入到小数点后3位,或0.000

答案 1 :(得分:0)

您可以使用 TRUNCATE 选项:

TRUNCATE((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,

如果您正在寻找舍入(如doublesharp所建议的那样),请使用ROUND

ROUND((A.QUANTITY_OUTSTANDING*100/NULLIF(A.INPUT_QTY_STD,0)), 3) as variance,

答案 2 :(得分:0)

使用Convert将其转换为所需长度的小数是我更喜欢的,当我实际上没有舍入值时,只是格式化。

 CONVERT(DECIMAL(10,3),10000)