sql >> Base de Datos >  >> RDS >> Mysql

consulta sql con sentencia if

Escribiría la consulta de esta manera:

SELECT o.name AS offer, COUNT(c.id) AS clicks, 
  SUM( IF(c.conversionDate IS NOT NULL, o.commission, NULL) ) AS revenue 
FROM offers o JOIN clicks c ON (c.offerID=o.ID)
GROUP BY o.ID;

Aquí hay otra solución, pero ofertas que no tienen ninguna los clics convertidos no se muestran en el resultado de la consulta:

SELECT o.name AS offer, COUNT(c.id) AS clicks, 
  SUM(o.commission) AS revenue 
FROM offers o JOIN clicks c 
  ON (c.offerID=o.ID AND c.conversionDate IS NOT NULL)
GROUP BY o.ID;