Home > front end >  The third column is not executed on my code
The third column is not executed on my code

Time:07-27

I am calculating a percentage in t3 on the same table by doing the ratio t1/t2, unfortunately I have no result. Can you help me fix this code?

Thanks in advance

select 
( --Echantillon en test
 SELECT count(*) nombre_enreg
 FROM prdiction_AVC1) as t1
( --Nombre total AVC(H/F)
select count(*)
from prdiction_AVC1
where stroke=1) as t2

Select 
       --Echantillon en test
    (( SELECT count(*) nombre_enreg
       FROM prdiction_AVC1) Echantillon
           /
         ( --Nombre total AVC(H/F)
        select count(*)
        from prdiction_AVC1
        where stroke=1) Total_avc))*100 as t3
 from t1,t2
 
 from prdiction_AVC1;

CodePudding user response:

APH basically answered this with his/her first comment, but this is what you can do:

select 
 count(*) as total, 
 count(case when stroke = 1 then 1 end) as sub_total,
 count(case when stroke = 1 then 1 end)*100.0/count(*) as sub_pct
from prdiction_AVC1

Output:

total  sub_total  sub_pct
3      1          33.333333

CodePudding user response:

I express myself differently. I want to create a third column on the same table. This column should calculate the percentage ratio by doing (t2/t1)*100

If I stop at the first two requests, everything is fine, unfortunately when I add the third, I get a blocking message, so the third column is not displayed.

SELECT --Nombre d'individus en test(H/F) (SELECT count(*) FROM prdiction_AVC1) as t1,

     --Nombre total AVC(H/F)
     (select count(*)
      from prdiction_AVC1
      where stroke=1) as t2,
      
      select t2/t1*100 as t3
       from t1,t2       

from prdiction_AVC1;

  •  Tags:  
  • sql
  • Related