我想计算两个日期(LeaseStart& Lease End)之间的完成百分比。
当前%显示图像中的%Perentage列只是一个演示数据。
在第4行,它显示100%,因为租约结束日期是今年4月,我今天就跑了。
因此,每当我根据今天运行此报告时,系统应计算LeaseStart& LeaseEnd日期并显示%。
请帮忙
由于
答案 0 :(得分:3)
DECLARE @table as TABLE(SR INT,LeaseStart DATE,LeaseEnd DATE)
INSERT INTO @table VALUES
(1,'2017-01-01','2021-12-31'),
(2,'2016-10-01','2017-12-31'),
(3,'2013-07-29','2018-06-30'),
(4,'2016-09-13','2017-04-07')
SELECT SR,LeaseStart,LeaseEnd,
DATEDIFF(DAY,LeaseStart,CASE WHEN GETDATE()<LeaseEnd THEN GETDATE() ELSE LeaseEnd END) DaysSoFar,
DATEDIFF(DAY,LeaseStart,LeaseEnd) TotalLease,
CAST((DATEDIFF(DAY,LeaseStart,CASE WHEN GETDATE()<LeaseEnd THEN GETDATE() ELSE LeaseEnd END)*1.00)/
(DATEDIFF(DAY,LeaseStart,LeaseEnd)*1.00)*100.00 AS DECIMAL(6,2)) AS Percentage
FROM @table
将结果
答案 1 :(得分:1)
不确定我同意您显示的结果,但请考虑以下
示例强>
Declare @YourTable table (SRNo int,LeaseStart date,LeaseEnd date)
Insert Into @YourTable values
(1,'2017-01-01','2021-12-31')
,(2,'2016-10-01','2017-12-31')
,(3,'2013-07-29','2018-03-30')
,(4,'2016-09-13','2017-04-07')
,(5,'2017-02-01','2019-01-31')
Select SRNo
,Pct = (DateDiff(DAY,LeaseStart,IIF(GetDate()>=LeaseEnd,LeaseEnd,GetDate()))*100.0)/DateDiff(DAY,LeaseStart,LeaseEnd)
From @YourTable
<强>返回强>
SRNo Pct
1 10.520547945205
2 62.280701754385
3 84.692082111436
4 100.000000000000
5 22.085048010973