在计算列中显示小数

时间:2018-07-16 20:25:12

标签: sql select count

我有以下有效的代码,但我希望“ pcttotalorders”列具有4个小数位。 enter image description here

select
    vendorno as 'Vendor', count (*) as 'Number Of Orders', count (*)*100/(
                                                                            select
                                                                                count(*)
                                                                            from
                                                                                mas_tfi.dbo.ap_invoicehistoryheader
                                                                                                                    ) as pcttotalorders
from
    mas_tfi.dbo.ap_invoicehistoryheader
group by
    vendorno
order by
    'Number of Orders' desc;

2 个答案:

答案 0 :(得分:2)

这似乎是此问题的重复: Write a number with two decimal places SQL server 只需更改十进制函数的参数,以便选择列中的列看起来像-

CONVERT(
        DECIMAL(10,4), 
        (COUNT(*)*100/(SELECT COUNT(*) FROM mas_tfi.dbo.ap_invoicehistoryheader))                                                                                                                   
        ) AS pcttotalorders

答案 1 :(得分:1)

尝试使用SQL Server格式功能

Select
vendorno as 'Vendor', count (*) as 'Number Of Orders',   
 Format(count (*)*100/(
                                                                        select
                                                                            count(*)
                                                                        from

mas_tfi.dbo.ap_invoicehistoryheader
                                                                                                                ), 
"##.####") as pcttotalorders
from
mas_tfi.dbo.ap_invoicehistoryheader
Group by
vendorno
order by
'Number of Orders' desc;

参考 https://docs.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-2017