当前位置: 首页 > 知识库问答 >
问题:

比较两个日期的行的SQL百分比

施阎宝
2023-03-14

我有包含行的表incident

TICKETID   ACTUALFINISH          TARGETFINISH
100      2012-03-01 11:11:11    2012-02-01 11:11:11 
101      2012-03-01 11:11:11    2012-01-01 11:11:11 
102      2012-04-01 11:11:11    2012-06-01 11:11:11 
103      2012-05-01 11:11:11    2012-07-01 11:11:11 
104        null                        null

我想拥有目标完成大于实际完成且相反的行的百分比。

SLA    PERCENTAGE
YES    50
NO     50
with HELPTABLE as
    (select count(*) as Total

from incident as incident4

where incident4.targetfinish is not null and incident4.actualfinish is not null )


select distinct  case when incident.targetfinish>incident.actualfinish  then 
                dec((( select count(*)

 from incident as incident1

 where  incident1.targetfinish is not null and incident1.actualfinish is not null  )),10,2)/HELPTABLE.Total*100

 when incident.targetfinish<incident.actualfinish  then 

                dec(((select count(*)

from incident as incident2

where incident2.targetfinish is not null and incident2.actualfinish is not null  )),10,2)/HELPTABLE.Total*100
                    end as Percentage,

        case when incident.targetfinish>incident.actualfinish then 'Yes'
             when incident.targetfinish<incident.actualfinish then 'No'
        end as SLA



from incident

where  incident.targetfinish is not null and incident.actualfinish is not null

如果有人知道什么是错误,谢谢!

[错误代码:-206,SQL状态:42703]DB2 SQL错误:sqlcode=-206,sqlstate=42703,sqlerrmc=helptable.total,driver=3.57.82)

共有1个答案

狄子真
2023-03-14
select 'YES' as SLA, 
(SUM(case when targetfinish > actualfinish  then 1.0 else 0.0 end) / count(*) ) * 100 as PERCENTAGE 
from incident 
where targetfinish is not null and actualfinish is not null
union 
select 'NO' as SLA, 
(SUM(case when targetfinish <= actualfinish  then 1.0 else 0.0 end) / count(*) ) * 100 as PERCENTAGE 
from incident
where targetfinish is not null and actualfinish is not null

http://sqlfiddle.com/#!3/2 E903/18

 类似资料:
  • 问题内容: 我需要在Java中比较两个日期。我正在使用这样的代码: 这是行不通的。变量的内容如下: 包含 包含 我该如何解决? 问题答案: 日期相等性取决于两个等于毫秒的日期。使用创建新的Date对象将永远不会等于过去创建的日期。Joda Time 的API简化了日期的处理。但是,仅使用Java的SDK:

  • 问题内容: 有人可以建议一种使用JavaScript 比较 两个 大于,小于和过去的 日期 的值的方法吗?这些值将来自文本框。 问题答案: 该Date对象会做你想要的东西- 构造一个每个日期,然后用它们进行比较,,或。 在,,,和运营商要求使用作为 要清楚的是,仅直接检查日期对象是否相等将不起作用 我建议您使用下拉列表或日期输入的某些类似约束形式,而不要使用文本框,以免您陷入输入验证的困境。

  • 问题内容: 如何使用Python比较两个日期以查看稍后的日期? 例如,我想检查当前日期是否超过了我创建的假期日期列表中的最后日期,因此它将自动发送电子邮件,告诉管理员更新holiday.txt文件。 问题答案: 使用方法和运算符及其种类。

  • 问题内容: 在我的收藏夹中,每个文档都有2个修改和同步的日期。我想查找已修改>同步,或不存在同步的内容。 我试过了 但没有显示我的期望。有任何想法吗? 谢谢 问题答案: 您不能将一个字段与具有正常查询匹配的另一个字段的值进行比较。但是,您可以使用聚合框架执行此操作: 我在其中输入“您的其他常规查询”,因为您可以使该位使用索引。因此,如果您只想对该字段所在的文档执行此操作,则可以执行以下操作: 用输

  • 问题内容: 请有人帮忙…为什么查询不起作用.. ?? 问题答案: 在Oracle中,一个总有一个时间成分。您的客户端可能显示也可能不显示时间分量,但是当您尝试进行相等比较时,它仍然存在。您还总是希望将日期与日期而不是字符串进行比较,后者使用当前会话进行隐式转换,从而使它们相当脆弱。THat将涉及ANSI日期文字或显式调用 您可以使用该函数将截断到午夜 或者,您可以进行范围比较(如果您可以受益于的索

  • 问题内容: 我想有一个compareTo方法,它忽略java.util.Date的时间部分。我想有很多方法可以解决这个问题。最简单的方法是什么? 问题答案: 虽然Joda Time当时是一个不错的建议,但请java.time尽可能使用Java 8+中的库。 我的首选是使用Joda Time,这使此操作变得异常简单: 编辑:如注释中所述,如果你使用DateTimeComparator.getDate