我需要在查询中创建一个子句

时间:2017-11-22 14:08:42

标签: mysql sql

我有这个代码,我不太了解mysql,需要过滤结果。

SELECT reinc, 
       Round(( Count(reinc) * 1.0 / (SELECT Count(1) 
                                     FROM   `gi35` 
                                     WHERE  fi_auto = 'S' 
                                            AND ( uf = 'CE' 
                                                   OR uf = 'MA' 
                                                   OR uf = 'PI' ) 
                                            AND periodo = '01/10/2017') ) * 100, 
       2) AS 
       porcento, 
       Count(reinc) 
FROM   `gi35` 
WHERE  fi_auto = 'S' 
       AND ( uf = 'CE' 
              OR uf = 'MA' 
              OR uf = 'PI' ) 
       AND periodo = '01/10/2017' 
GROUP  BY reinc 

此代码返回:

enter image description here

我只想获得值40,92

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

你似乎想要:

SELECT Round( Count(*) * 1.0 /
              (SELECT Count(*) 
               FROM `gi35` g2
               WHERE g2.fi_auto = 'S' AND g2.uf IN ('CE', 'MA', 'PI')
                     g2.periodo = '2017-10-01'
              ) * 100, 2
             ) AS porcento
FROM `gi35` g
WHERE g.fi_auto = 'S' AND g.uf IN ('CE', 'MA', 'PI') AND
      g.periodo = '2017-10-01' AND reinc = 'S';

我认为periodo是一个日期,并使用标准的,非模糊的日期格式。

您还可以将查询缩短为:

SELECT Round( AVG(reinc = 'S') * 100, 2) AS porcento
FROM `gi35` g
WHERE g.fi_auto = 'S' AND g.uf IN ('CE', 'MA', 'PI') AND
      g.periodo = '2017-10-01' AND reinc = 'S';