Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

MySQL select sum com group by e valores vazios


Você pode usar isso:
SELECT b.store_id, b.group_hour, IFNULL(sales_sum, 0) AS sales_sum
FROM 
(
 SELECT store_id, HOUR(sales_date) as group_hour, sum(sales_amount) as sales_sum 
 FROM sales 
 GROUP BY store_id, group_hour
) a
RIGHT OUTER JOIN 
(
  SELECT *
  FROM
  (
   SELECT DISTINCT store_id
   FROM sales
  ) all_stores
  CROSS JOIN
  (
   SELECT 10 as group_hour
   UNION ALL
   SELECT 11
   UNION ALL
   SELECT 12
   UNION ALL
   SELECT 13
   UNION ALL
   SELECT 14
   UNION ALL
   SELECT 15
  ) all_hours
) b
ON a.store_id = b.store_id AND a.group_hour = b.group_hour
ORDER BY 1, 2

Estou usando RIGHT OUTER JOIN com a tabela que contém todas as lojas e todas as horas (de 10 a 15).