以2位小数显示SQL中的计算结果
P粉055726146
P粉055726146 2024-04-02 14:23:34
0
1
429

我编写了一个简单的查询来查找两列的百分比,但我希望结果在只有两位小数的列中。这是我写的,但我遇到了错误,而且我似乎无法修复它。

SELECT 
    Small_Bags,
    Total_Bags,
    (Small_Bags / Total_Bags) * 100 AS Small_Bags_Percent
    (Select CAST (Small_Bags_Percent AS DECIMAL (10,2)))
FROM Table_Bags
WHERE Total_Bags <>0

P粉055726146
P粉055726146

全部回复(1)
P粉653045807

您不能在同一查询中使用列别名。你可以做到这一点。

select Small_Bags
    , Total_Bags
    , (Small_Bags / Total_Bags) * 100 AS Small_Bags_Percent
    , cast(((Small_Bags / Total_Bags) * 100) as decimal(10,2))
from Table_Bags
where Total_Bags > 0

或者使用子查询

select t1.Small_Bags, t1.Total_Bags, t1.Small_Bags_Percent
    , cast(t1.Small_Bags_Percent as decimal(10,2))
from 
    (select Small_Bags
        , Total_Bags
        , (Small_Bags / Total_Bags) * 100 AS Small_Bags_Percent
        , cast(((Small_Bags / Total_Bags) * 100) as decimal(10,2))
    from Table_Bags
    where Total_Bags > 0) t1
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板