2012-06-30 166 views
0

我有表INCIDENT與行SQL百分比

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 finishactual finish和相對較大。

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

SLA PERCENTAGE 
YES 50 
NO  50 

我寫的SQL查詢,但我不斷收到錯誤。我不知道錯誤在哪裏。

我第一次得到的不是地方AFIS比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 

如果有人知道什麼是錯誤的感謝小!

[Error Code: -206, SQL State: 42703] DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703, SQLERRMC=HELPTABLE.TOTAL, DRIVER=3.57.82)

回答

2
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

+0

謝謝,但我不斷收到0回報此值。我嘗試只選擇'是'作爲SLA, (SUM(當targetfinish> actualfinish然後1 else 0結束時)/ count(*))* 100作爲PERCENTAGE 來自事件 其中targetfinish不爲null且actualfinish不爲null我得到0回報? – Dejan

+0

這不行。 SQL中缺少什麼? – Dejan

+0

@Dejan右鍵,我編輯:把1.0(和0,0)或者分配一個分區的一部分,因爲float會起作用。如果你不這樣做,它是一個整數除法,並且在整數中,2/5 = 0 –