2012-06-30 6 views
0

J'ai la table INCIDENT avec des lignespourcentage de lignes SQL comparant deux dates

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 

Je veux avoir en pourcentage de lignes où target finish est plus grand que actual finish et en face.

Donc, pour ce résultat de la table sera (ne comportant pas les valeurs nulles):

SLA PERCENTAGE 
YES 50 
NO  50 

j'ai écrit requête SQL, mais je continue à avoir une erreur. Je ne sais pas où est l'erreur.

j'obtenir d'abord nombre total d'enregistrements que AFIS où plus gros que TF et AF est inférieur à 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 

Si quelqu'un sait ce qui est la grâce d'erreur!

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

Répondre

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

Merci, mais je continue à obtenir 0 retour pour cette valeur. J'ai essayé avec seulement sélectionner 'OUI' comme SLA, (SUM (cas où targetfinish> actualfinish puis 1 sinon 0 end)/count (*)) * 100 comme PERCENTAGE où l'incident targetfinish n'est pas null et actualfinish n'est pas null et je reçois 0 pour le retour? – Dejan

+0

Cela ne fonctionne pas. Quelque chose manque-t-il dans SQL? – Dejan

+0

@ Dejan droite, j'ai édité: mettre 1.0 (et 0,0) ou lancer une partie de la division comme float fera l'affaire. Si ce n'est pas le cas, c'est une division entière, et en entier, 2/5 = 0 –