需要帮助将“Pct_To_Total”列的值转换为
John, 0,36
Sophia, 0,29
Stella, 0,18
Jennifer, 0,11
仅需要两位小数,如果第三位小数大于值4,则需要将第二位小数转换为更高的值。
declare @T table
(
Name varchar(50),
Sales decimal
)
insert into @T values
('John', '50'),
('Sophia', '40'),
('Stella', '25'),
('Jennifer', '15'),
('Greg', '10')
SELECT a1.Name, a1.Sales, (a1.Sales /(SELECT SUM(Sales) FROM @T Total_Sales) ) AS Pct_To_Total
FROM @T a1, @T a2
WHERE a1.Sales <= a2.sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;
答案 0 :(得分:1)
答案 1 :(得分:1)
SELECT round (cast(0.35714285 AS DECIMAL(10,2)),2);
这会给0.36
您的例外结果。
我尝试了以下查询,它运行正常。
SELECT a1.Name, a1.Sales, round(cast((a1.Sales /(SELECT SUM(Sales) FROM @T Total_Sales)) AS DECIMAL(10,2)),2) AS Pct_To_Total
FROM @T a1, @T a2
WHERE a1.Sales <= a2.sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;
答案 2 :(得分:0)
答案 3 :(得分:0)
SELECT ROUND(0.36, 2) = 0.36
SELECT ROUND(0.364, 2) = 0.36
SELECT ROUND(0.365, 2) = 0.37