1

Quiero dividir dos resultados de dos consultos MySQL

SELECT `count_first_recurrent`/`count_initialization` FROM
    SELECT count(distinct `orders`.`gxo_vads_subscription`) AS `count_first_recurrent`
    FROM `orders`
    WHERE (`orders`.`engagement` = 'R'
       AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'SE'),
    SELECT count(distinct `orders`.`gxo_vads_subscription`) AS `count_initialization`
    FROM `orders`
    WHERE (`orders`.`engagement` = 'M'
       AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'SE')

En efecto cada uno de los inner select devuelven numeros y quiero dividirlos.

Por desgracia lo que intenté me devuelve:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT count(distinct `orders`.`gxo_vads_subscription`) AS `count_first_recurren' at line 3
   

Actualizacion

Con lo que me aconseja Srsole, intenté:

SELECT (count_first_recurrent/count_initialization) FROM
    (
        (SELECT count(distinct `orders`.`gxo_vads_subscription`) count_first_recurrent
        FROM `orders`
        WHERE `orders`.`engagement` = 'R'
           AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'SE') as a,
           
        (SELECT count(distinct `orders`.`gxo_vads_subscription`) count_initialization,
        FROM `orders`
        WHERE `orders`.`engagement` = 'M'
           AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'SE') AS b
   )

Pero me devuelve:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `orders` WHERE `orders`.`engagement` = 'M' AND `orders' at line 20

2 respuestas 2

1

Basado en tu enunciado he hecho esto:

SELECT (count_first_recurrent/count_initialization) FROM
    ((SELECT 15 count_first_recurrent)as  a,
    (SELECT 3 count_initialization)as b)

Básicamente falta el tema de los paréntesis y poner alias a cada consulta.


Actualización

Te lo paso en limpio y con tu ejemplo

SELECT (count_first_recurrent/count_initialization) FROM
    (
        (SELECT count(distinct `orders`.`gxo_vads_subscription`) count_first_recurrent
        FROM `orders`
        WHERE `orders`.`engagement` = 'R'
           AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'SE') as a,
           
        (SELECT count(distinct `orders`.`gxo_vads_subscription`) count_initialization
        FROM `orders`
        WHERE `orders`.`engagement` = 'M'
           AND `orders`.`status` = 2 AND `orders`.`gxo_means` = 'SE') AS b
   )

Ten en cuenta que he puesto count_first_recurrent y count_initialization en el orden que a mi ha parecido, cámbialo como convenga.

Tenia una coma de mas, esta borrada.

7
  • Muchas gracias ! Ya lo intenté pero me parece que ha desplazado el problema Commented el 25 jun. 2021 a las 9:25
  • @RevolucionforMonica te faltan los alias de los campos
    – Srsole
    Commented el 25 jun. 2021 a las 9:37
  • estoy viendo que te has liado con los parentesis
    – Srsole
    Commented el 25 jun. 2021 a las 9:40
  • @RevolucionforMonica ya lo tienes
    – Srsole
    Commented el 25 jun. 2021 a las 9:44
  • Muchas gracias ! Sin embargo me devuelve que hay un error de sintaxis cerca de 'FROM orders WHERE orders.engagement = 'M' AND `orders' a la linea 8 Commented el 25 jun. 2021 a las 9:53
0

Otras formas similares de resolverlo:

Query 1:

SELECT 
       SUM(CASE WHEN engagement = 'R' THEN 1 END) / SUM(CASE WHEN engagement = 'M' THEN 1 END)
FROM (
      SELECT DISTINCT engagement, gxo_vads_subscription
      FROM orders
      WHERE engagement IN ('R', 'M') AND status = 2 AND gxo_means = 'SE') AS sq

Query 2:

SELECT (SELECT COUNT(DISTINCT gxo_vads_subscription)
        FROM orders
        WHERE engagement = 'R' AND status = 2 AND gxo_means = 'SE') / 
        
        (SELECT COUNT(DISTINCT gxo_vads_subscription)
         FROM orders
         WHERE engagement = 'M' AND status = 2 AND gxo_means = 'SE')

Query 3:

SELECT SUM(count_first_recurrent) / SUM(count_initialization)
FROM (
      SELECT CASE WHEN engagement = 'R' THEN 1 END AS count_first_recurrent,
             CASE WHEN engagement = 'M' THEN 1 END AS count_initialization
      FROM orders
      WHERE engagement IN ('R', 'M') AND status = 2 AND gxo_means = 'SE'
      GROUP BY engagement, gxo_vads_subscription) AS sq

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.