2012-06-30 4 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 finish больше, чем actual finish и напротив.

Так это таблица результатов будет (не включая нулевые значения):

SLA PERCENTAGE 
YES 50 
NO  50 

я написал SQL запрос, но я получаю сообщение об ошибке. Я не знаю, где ошибка.

Я первый получать общее количество записей, чем где АДИС, больше чем 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 (case when targetfinish> actualfinish then 1 else 0 end)/count (*)) * 100 как PERCENTAGE от инцидента , где targetfinish не является нулевым и фактическим значением не является null и я получаю 0 за возвращение? – Dejan

+0

Это не работает. Что-то не хватает в SQL? – Dejan

+0

@Dejan right, я отредактировал: put 1.0 (и 0,0) или отбросил одну часть деления, так как float сделает трюк. Если вы этого не сделаете, это целочисленное деление и в целых числах 2/5 = 0 –

Смежные вопросы