在 SQL 服务器中细分一年中每个季度的进度(百分比)
Breaking down progress(Percentage) through each quarter of the year in SQL Server
基本上我正在尝试计算当前季度的进度,以百分比表示。目前我有:
(DATEPART(dd,@AsOf)/91) * 100
我们使用 91 天作为本季度的固定解决方案。他们不需要 100% 的准确性。
@AsOf
作为 DATETIME
类型传入。
我尝试了多种方法,但收到 0。我认为这是因为我使用的是 INT
而不是 DECIMAL
,但我试过了,但仍然收到 0。
您应该可以通过添加小数点将其强制为小数,例如 91.0
和 100.0
以避免 integer division 问题:
DECLARE @date DATETIME
set @date = getdate();
select DATEPART(dd,@date) TheDay,
(DATEPART(dd,@date)/91.0) DivBy91,
(DATEPART(dd,@date)/91.0) * 100.0 Result
结果:
TheDay DivBy91 Result
19 0.208791 20,8791000
If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.
整数除法会在第 2 列中产生 0
,这就是导致结果为 0 的原因。
基本上我正在尝试计算当前季度的进度,以百分比表示。目前我有:
(DATEPART(dd,@AsOf)/91) * 100
我们使用 91 天作为本季度的固定解决方案。他们不需要 100% 的准确性。
@AsOf
作为 DATETIME
类型传入。
我尝试了多种方法,但收到 0。我认为这是因为我使用的是 INT
而不是 DECIMAL
,但我试过了,但仍然收到 0。
您应该可以通过添加小数点将其强制为小数,例如 91.0
和 100.0
以避免 integer division 问题:
DECLARE @date DATETIME
set @date = getdate();
select DATEPART(dd,@date) TheDay,
(DATEPART(dd,@date)/91.0) DivBy91,
(DATEPART(dd,@date)/91.0) * 100.0 Result
结果:
TheDay DivBy91 Result
19 0.208791 20,8791000
If an integer dividend is divided by an integer divisor, the result is an integer that has any fractional part of the result truncated.
整数除法会在第 2 列中产生 0
,这就是导致结果为 0 的原因。