ROUND T-SQL的值为0.0

时间:2016-12-04 18:02:07

标签: sql-server tsql

我正在使用Select语句,我必须显示类型的平均值,但结果必须显示0值为0.0

我的代码是

SELECT genreName AS 'Genre'
       , CAST(CASE WHEN
                   AVG(rateValue) IS NULL
                   THEN 0
               ELSE ROUND(FORMAT(AVG(rateValue),2),0.0)
               END AS FLOAT) AS 'Average Rating'
FROM [IMDB].[FilmGenre] flmgnr
     LEFT JOIN [IMDB].[FilmGenreAllocation] flmgnrall
     ON(flmgnrall.genreId = flmgnr.genreId)
     LEFT JOIN [IMDB].[Film] flm
     ON(flm.filmId = flmgnrall.filmId)
     LEFT JOIN [IMDB].[FilmReview] flmrvw
     ON(flmrvw.filmId = flmgnrall.filmId)
GROUP BY genreName;
GO

,结果必须是

+---------+----------------+
|  Genre  | Average Rating |
+---------+----------------+
| Action  | 0.00           |
| Comedy  | 4.67           |
| Crime   | 4.50           |
| Drama   | 4.50           |
| Family  | 0.00           |
| Mystery | 4.40           |
+---------+----------------+

2 个答案:

答案 0 :(得分:0)

不要将你的价值观转为浮动,而是像DECIMAL(18,2)那样:

CAST(CASE WHEN
               AVG(rateValue) IS NULL
               THEN 0
           ELSE ROUND(FORMAT(AVG(rateValue),2),0.0)
           END AS DECIMAL(18,2)) AS 'Average Rating'

答案 1 :(得分:0)

而不是

twig:
    cache: false

$kernel = new AppKernel('dev', true);

使用0告诉SQL Server - 大小写是int数据类型。

或者更好的方式:

THEN 0

请:

THEN 0.0