Good evening. I have two queries of the following form:

SELECT COUNT(*) AS firtsRes FROM tableName 

and second:

 SELECT COUNT(*) AS secondRes FROM tableName WHERE fieldName > someValue 

I need to get the result of dividing firstRes / secondRes. Is it possible to get it in one request, in other words, merge the two above requests into one? Thank you in advance

  • 2
    @Andy_Reed select count (*) / sum (case when fieldName> someValue then 1 else 0 end) from tableName; - alexlz

2 answers 2

 SELECT COUNT(*)/(SELECT COUNT(*) FROM tableName WHERE fieldName > someValue) FROM tableName 

    Try this:

     SELECT COUNT(*)/`t2`.`secondRes` AS `itogo` FROM `table_1` JOIN ( SELECT COUNT( * ) AS `secondRes` FROM `table_2` WHERE `fieldName` > `someValue` ) t2 

    PS Above I wrote a query for two tables, but also for one - it also works

    • Careful, please. What are table_1, table_2? - alexlz
    • @alexlz, and what should be more careful? - Deonis
    • In the job, the table name is tableName (one). You have FROM table_1 and table_2 (two). Or I misunderstood the vehicle, or you. - alexlz 5:53 pm
    • @alexlz, in fact, is not difficult to see that in addition, I pointed out that for one it also works. - Deonis
    • I apologize. - alexlz 5:59 pm