2

我有一张包含资源信息的表格,基本信息是:

ID    Total    Start        End          Used
----------------------------------------------
1     350      01-01-2012   31-12-2012   80.6%
2     250      01-01-2012   31-12-2012   51.5%
3     3500     01-01-2012   31-07-2013   12.5%
4     350      01-01-2012   31-10-2012   91.0%

列是:

  • Total-- 资源总数(货币、时间、纸张等)。
  • Start-- 资源开始日期
  • End-- 资源结束日期
  • Used-- 迄今为止使用的资源百分比

我必须尝试计算(或估计)资源何时会以目前使用的速度耗尽。

我尝试了几种不同的方法,使用使用的百分比和百分比,但没有任何意义,我很确定有一种简单的方法可以做到这一点,但我找不到它。

我的理想输出将是下面的文本,但我可能会在应用程序中格式化:

You have used X% of your [resource name] in Y% of the time allotted, 
at this rate the resource will run down around [Run Down Date].

任何人都可以计算出这是如何计算的吗?

可以玩的 SQL Fiddle

编辑:

为了尝试使问题更清楚,我将解释如何计算单个日期:

对于第一行 (ID = 1)。

Average % per day = Percentage (80.6) / Days between Start and Today (205)
Average % per day = 0.003931707%

% remaining = Percentage (80.6%)
% remaining = 19.4%

Days remaining = Average % per day (0.003931707%) / % remaining (19.4%)
Days remaining = 49.34243176

Project Run Down = Today + Days Remaining (49.34243176)
Project Run Down = 11/09/2012 (11th Sep)

我已尝试将此过程转换为 SQL,但无法正常工作。

4

3 回答 3

2

你可以试试这样的

declare @res table(
    id int identity(1,1)
    ,total int
    ,start date
    ,[end] date
    ,used float )


insert into @res(total, start , [end], used) 
values
    (350, '20120101', '20121231', 0.806)
    ,(250, '20120101', '20121231', 0.515)   


select 
*
,used/DATEDIFF(DAY,start,GETDATE()) as avUsePerDay
,1/(used/DATEDIFF(DAY,start,GETDATE())) as expectedDaysTotal
,DATEADD(day,1/(used/DATEDIFF(DAY,start,GETDATE())),start) as expectedToDie
from @res
于 2012-07-24T14:48:59.260 回答
1

那将是...

SELECT DATEDIFF(d, [StartDate], GETDATE()) * 100.0 /  DATEDIFF(d, [StartDate], [EndDate]) AS PercentageTimeGone,
       DATEADD(d, (100 - USED) / (Used / DATEDIFF(d, [StartDate], GETDATE())), GETDATE()) AS ProjectedEndingDate
FROM Resources 
于 2012-07-24T13:51:19.860 回答
0

关于什么

select id, total, start, end, datediff(day,start,getdate()) / ( total / 100 )

我不擅长计算,但在数字上它是有效的。

祝一切顺利

于 2012-07-24T13:43:52.787 回答