SQL - 根据另一列中的值更改计算结果

时间:2016-05-16 15:53:02

标签: sql percentage

我有一个百分比列,我根据另外两列计算。但是,根据另一列中的值,我可以更改该计算。

例如

server  outage  starttime         endtime           outage_seconds percentage
a       0       01/11/2015 09:14  01/11/2015 09:08  360            99.58334
b       0       01/11/2015 03:57  01/11/2015 03:34  1393           98.38774
c       1       01/11/2015 03:47  01/11/2015 03:29  1050           98.78473
d       0       01/11/2015 03:47  01/11/2015 03:29  1086           98.74306
e       1       01/11/2015 03:47  01/11/2015 03:29  1056           98.77778

如果中断列中的数据显示为0,我希望百分比列表示100%

我的代码是

SELECT [server]
  ,[outage]
  ,[starttime]
  ,[endtime]
  ,[outage_seconds] = datediff(second, 0, cast((endime - starttime)as time))
  ,[percentage] = 100-(datediff(second, 0, cast((endtime - starttime)as float))/86400.0)*100
FROM [availability_outages].[dbo].[pnet02_view]

1 个答案:

答案 0 :(得分:1)

SELECT [server]
  ,[outage]
  ,[starttime]
  ,[endtime]
  ,[outage_seconds] = datediff(second, 0, cast((endime - starttime)as time))
  ,case when [outage] = 0 
        then 100
        else 100-(datediff(second, 0, cast((endtime - starttime)as float))/86400.0)*100
   end as percentage
FROM [availability_outages].[dbo].[pnet02_view]
相关问题