我正在尝试使用DECIMAL(5,2)数据类型更新列。但是,SQL Server似乎是四舍五入而不是存储小数位。
例如,其中一个值为1.53,并在表中存储为1.00。
这是查询
DECLARE @MaxLike DECIMAL (5,2), @MaxComment DECIMAL (5,2), @MaxFavourite DECIMAL (5,2)
SET @MaxLike = 1.0*100/(SELECT MAX(z.MaxLike) FROM (SELECT(COUNT(COALESCE(Liked,0))) AS MaxLike FROM Design_LikeRating GROUP BY DesignID) z)
SET @MaxComment = 1.0*100/(SELECT MAX(z.MaxComment) FROM (SELECT(COUNT(COALESCE(CommentID,0))) AS MaxComment FROM Comment_CommentDetail WHERE IsDeleted = 0 GROUP BY OverallParentGUID) z)
SET @MaxFavourite = 1.0*100/(SELECT MAX(z.MaxFavourite) FROM (SELECT (COUNT(COALESCE(DesignID,0))) AS MaxFavourite FROM Design_Favourite GROUP BY DesignID) z)
-- VoteCount and Rate
DECLARE @table1 TABLE (DesignID INT PRIMARY KEY, Rate DECIMAL(5,2))
INSERT INTO @table1
SELECT DesignID, CAST (Rate AS DECIMAL(5,2)) AS Rate
FROM
(
SELECT DesignID, COUNT(COALESCE(Liked,0.0)) AS Rate
FROM Design_LikeRating WITH (NOLOCK)
GROUP BY DesignID
) z
-- FavCount
DECLARE @table2 TABLE (DesignID INT PRIMARY KEY, FavCount DECIMAL(5,2))
INSERT INTO @table2
SELECT DesignID, CAST (FavCount AS DECIMAL (5,2)) AS FavCount
FROM
(
SELECT DesignID, COUNT(COALESCE(DesignID,0.0)) AS FavCount
FROM Design_Favourite WITH (NOLOCK)
GROUP BY DesignID
) x
-- CommentCount
DECLARE @table3 TABLE (DesignGUID UNIQUEIDENTIFIER PRIMARY KEY, CommentCount DECIMAL(5,2))
INSERT INTO @table3
SELECT OverallParentGUID, CAST (CommentCount AS DECIMAL(5,2)) AS CommentCount
FROM
(
SELECT OverallParentGUID, COUNT(DISTINCT COALESCE(x.DesignID,0.0)) AS CommentCount
FROM Comment_CommentDetail z WITH (NOLOCK)
INNER JOIN Design_DesignDetail x WITH (NOLOCK) ON x.DesignGUID = z.OverallParentGUID
WHERE z.IsDeleted = 0 AND x.UserIDInt != z.UserIDInt
GROUP BY OverallParentGUID
) y
;
WITH CTE AS
(
SELECT (COALESCE((1.0 * z.Rate*@MaxLike),0.0) + COALESCE((1.0 * y.FavCount*@MaxFavourite),0.0) + COALESCE((1.0 * x.CommentCount*@MaxFavourite),0.0)) AS PopularityScore, a.DesignID
FROM Design_DesignDetail a
LEFT JOIN @table1 AS z ON z.DesignID = a.DesignID
LEFT JOIN @table2 AS y ON y.DesignID = a.DesignID
LEFT JOIN @table3 AS x ON x.DesignGUID = a.DesignGUID
WHERE DesignID > 300000 and DesignID = 444409
)
UPDATE Design_DesignDetail
SET PopularityScore = z.PopularityScore
FROM Design_DesignDetail a
INNER JOIN CTE z on z.DesignID = a.DesignID
更新 我会停止尝试聪明并在这里发布整个查询。正如你所看到的,它得到了一点点每个人的答案,但它仍然没有用!
答案 0 :(得分:4)
此表达式必须为int或小数,其标度为0(无dec dec)
(ISNULL((z.Rate*@MaxLike),0) + ISNULL((y.FavCount*@MaxFavourite),0) + ISNULL((x.CommentCount*@MaxFavourite),0))
我猜你需要CAST才能在不知道数据类型的情况下在每个ISNULL中浮动
请不要像那样使用NOLOCK
完整代码更新后编辑
您的COUNT
聚合将返回int值,因此您的小数(5,2)值可能包含xxx.00。
有些评论已添加到@ table1 loading
INSERT INTO @table1
SELECT DesignID,
-- gbn The CAST changes 2 to 2.00
CAST (Rate AS DECIMAL(5,2)) AS Rate
FROM
(
SELECT DesignID,
-- gbn COALESCE does nothing. COUNT gives int and simply counts non-null values
COUNT(COALESCE(Liked,0.0)) AS Rate
FROM Design_LikeRating WITH (NOLOCK)
GROUP BY DesignID
) z
有关详细说明,请参阅我的答案Count(*) vs Count(1)
答案 1 :(得分:1)
你可能会遇到整数数学的某些问题。
除非你喜欢舍入错误,否则在进行计算时永远不要使用浮点数。
答案 2 :(得分:1)
z.Rate,@ MaxLike,y.FavCount,@ MaxFavourite和x.CommentCount有哪些类型?
我打赌y.FavCount和x.CommentCount是整数。
答案 3 :(得分:0)
SELECT ISNULL(1.0 * z.Rate*@MaxLike, 0.0) +
ISNULL(1.0 * y.FavCount*@MaxFavourite, 0.0) +
ISNULL(1.0 * x.CommentCount*@MaxFavourite, 0.0) AS PopularityScore, a.DesignID