簡體   English   中英

比較兩個日期的SQL百分比

[英]SQL percentage of rows comparing two dates

我有表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

我希望在target finish大於actual finish和相反光線的行數百分比。

因此,對於此表,結果將是(不涉及空值):

SLA    PERCENTAGE
YES    50
NO     50

我寫了SQL查詢,但我一直收到錯誤。 我不知道錯誤在哪里。

我首先得到的記錄總數大於AF大於TF的記錄,然后AF小於TF

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)

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/2e903/18

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM