简体   繁体   English

如何将SQL输出固定为两位小数

[英]How to fix SQL output into two decimal places

I have this SELECT statement: 我有这个SELECT语句:

  SELECT
     t.clmUnit,
     t.clmConType,
     Count(t.clmCaseID) TotalCases,
     SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END) TotalResolved,
     cast(SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END) as decimal(18,2))/cast(Count(t.clmCaseID) as decimal(18,2))*100 as [TotalResolved %],
    SUM(CASE WHEN t.clmOutcomeType='Unresolved' THEN 1 ELSE 0 END) TotalCaseProgressed,
    cast(SUM(CASE WHEN t.clmOutcomeType='Unresolved' THEN 1 ELSE 0 END) as decimal(18,2))/cast(Count(t.clmCaseID)as decimal(18,2))*100 as [TotalCaseProgressed %],
    SUM(CASE WHEN t.clmOutcomeType='Work In Progress' THEN 1 ELSE 0 END) TotalFurtherConReq,
    cast(SUM(CASE WHEN t.clmOutcomeType='Work In Progress' THEN 1 ELSE 0 END) as decimal(18,2))/cast(Count(t.clmCaseID)as decimal(18,2))*100 as [TotalFurtherConReq %]

Which shows the output columns as decimal figures (eg) 62.24837411 and I know I can put this into excel and format the column as a percentage, but my question is how do I get SQL to convert it to the percentage? 哪个将输出列显示为十进制数字(例如62.24837411),我知道可以将其放入excel并将列格式设置为百分比,但是我的问题是如何获取SQL并将其转换为百分比? I have tried changing the as decimal(18,2) to as decimal (4,2) which is what I want but it just throws the math overflow error? 我尝试将as十进制(18,2)更改为十进制(4,2),这是我想要的,但是它只会引发数学溢出错误?

Try this one : 试试这个:

select  convert(DECIMAL(10,2),column)

for precision of 2 decimal places 精确到小数点后两位

You are place the cast in a wrong place 您将cast放错了地方

cast(SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END) as decimal(18,2))/cast(Count(t.clmCaseID) as decimal(18,2))*100 as [TotalResolved %],

Place the CAST for the whole result, like the below: 为整个结果放置CAST ,如下所示:

cast(SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END)/cast(Count(t.clmCaseID) as decimal(18,2))*100  as decimal(18,2)) as [TotalResolved %],

AS Arul Suggested, you need to use cast function on the whole column calculation. 根据Arul建议,您需要在整个列计算中使用强制转换功能。

  SELECT
 t.clmUnit,
 t.clmConType,
 Count(t.clmCaseID) TotalCases,
 SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END) TotalResolved,
 cast(SUM(CASE WHEN t.clmOutcomeType='Resolved' THEN 1 ELSE 0 END))/Count(t.clmCaseID) * 100  as decimal(18,2)) as [TotalResolved %],
SUM(CASE WHEN t.clmOutcomeType='Unresolved' THEN 1 ELSE 0 END) TotalCaseProgressed,
cast(SUM(CASE WHEN t.clmOutcomeType='Unresolved' THEN 1 ELSE 0 END))/Count(t.clmCaseID) * 100 as decimal(18,2)) as [TotalCaseProgressed %],
SUM(CASE WHEN t.clmOutcomeType='Work In Progress' THEN 1 ELSE 0 END) TotalFurtherConReq,
cast(SUM(CASE WHEN t.clmOutcomeType='Work In Progress' THEN 1 ELSE 0 END))/Count(t.clmCaseID) * 100 as decimal(18,2)) as [TotalFurtherConReq %]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM