【问题标题】:MSSQL percentage calculation not consistentMSSQL百分比计算不一致
【发布时间】:2014-06-18 22:18:56
【问题描述】:

我需要计算两个数字之间的增长百分比。 在下面的示例中,起始值是第一个,当前值是第二个。 当我尝试以下查询时,结果不符合我的预期。

SELECT ( CAST (68 as float) / CAST (1 as float) * 100) --current value 68, start value 1, the percentage of growth is 68% --MSSQL is giving the result 6800, which is 100 times too high

SELECT ( CAST (30 as float) / CAST (10 as float) * 100) --current value 30, start value 10, the percentage of growth is 300% --MSSQL is giving the result 300, which makes sense

SELECT ( CAST (20 as float) / CAST (40 as float) * 100) --current value 20, start value 40, the percentage of growth is -50% --MSSQL is giving the result 50, which somewhat makes sense

我应该使用哪个查询来获得每种情况的一致输出,以便我可以使用它在图表中正确显示?

【问题讨论】:

    标签: percentage


    【解决方案1】:

    MySQL 计算正确。您的百​​分比增长计算(您的 cmets)看起来很奇怪。

    例如您的第一个示例:起始值 68,当前值 1。(68/1)*100 = 6800。如果您想要 68% 的增长,那么您的起始值需要为 1.68,而不是 68,这将使起始值达到 168%当前值。 IE。它增长了 68%。

    您的第三个示例:(20/40)*100 = (1/2)*100 = 50。起始值是当前值的一半,因此 50% 是正确的。

    【讨论】:

    • 我将 cmets 中的当前值和起始值颠倒了,现在修复了这个问题。但我仍然想知道我可以将哪个单一查询用于所有三值场景'。
    • 啊,是的,我现在看到了。 1 增长到 68 确实是 6800%。我虽然这只是 68% :-)。感谢您的回答!
    猜你喜欢
    • 2018-04-08
    • 2011-06-01
    • 2021-01-10
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2015-09-04
    相关资源
    最近更新 更多