SQL:显示的小数超出了要求

时间:2018-11-14 12:48:06

标签: sql select numbers decimal

这是我的代码。

SELECT Concat(Round(SUM(Coalesce(Price.Morning, Price.Day, 
Price.Evening)*Tickets.count),2),' €') AS 'Total',
Concat(Round(SUM(Price.Morning * Tickets.count) * 100 / 
(SUM(Coalesce(Price.Morning, Price.Day, Price.Evening)*Tickets.count)),2),'%') 
AS 'Riti',
Concat(Round(SUM(Price.Day * Tickets.count) * 100 / 
(SUM(Coalesce(Price.Morning, Price.Day, Price.Evening)*Tickets.count)),2),'%') 
AS 'Dienas',
Concat(Round(SUM(Price.Evening * Tickets.count) * 100 / 
(SUM(Coalesce(Price.Morning, Price.Day, 
Price.Evening)*Tickets.count)),2),'%') 
AS 'Evening'
FROM Price,Tickets
WHERE Tickets.Price_ID = Price.Price_ID
;

它显示如下:

image

一切正常,但逗号后只需要2位小数,但仍然显示2以上。

1 个答案:

答案 0 :(得分:0)

不要在SQL中进行数字格式化。让显示层(应用程序,报表,表单等)进行格式化。它限制了您适应不同文化,使用偏好等的能力。

只需返回原始数据并删除格式:

SELECT
    SUM(Coalesce(Price.Morning, Price.Day, Price.Evening)*Tickets.count) AS 'Total',
    SUM(Price.Morning * Tickets.count) / 
        SUM(Coalesce(Price.Morning, Price.Day, Price.Evening)*Tickets.count) AS 'Riti',
    SUM(Price.Day * Tickets.count) / 
       SUM(Coalesce(Price.Morning, Price.Day, Price.Evening)*Tickets.count) AS 'Dienas',
    SUM(Price.Evening * Tickets.count) / 
       SUM(Coalesce(Price.Morning, Price.Day, Price.Evening)*Tickets.count AS 'Evening'
FROM Price,Tickets
WHERE Tickets.Price_ID = Price.Price_ID

您还可以通过将重复计算Coalesce(Price.Morning, Price.Day, Price.Evening)*Tickets.count放在子查询中来简化此操作。