我如何通过连接产品和城市表获得最佳 mysql 分组结果?
How i get a best mysql grouping result with join product and city tables?
我连接了几个表来进行查询,City、Product、Marca 和 Sales,我需要按产品分组并按每个城市的销售额显示 de average
SELECT m.marca, p.producto,
case when ci.id_ciudad = 1 then AVG(a.valor) end as medellin,
case when ci.id_ciudad = 126 then AVG(a.valor) end as barranquilla,
case when ci.id_ciudad = 149 then AVG(a.valor) end as bogota,
case when ci.id_ciudad = 150 then AVG(a.valor) end as cartagena,
case when ci.id_ciudad = 1004 then AVG(a.valor) end as cali
FROM tbma_productos p
LEFT JOIN tbma_activacion_producto a on p.id_producto = a.id_producto
LEFT JOIN tbma_activaciones i on i.id_instalacion = a.id_activacion
LEFT JOIN tbax_almacenes c on c.id_almacen = i.id_punto_venta
LEFT JOIN tbax_ciudades ci on ci.id_ciudad = c.id_ciudad
LEFT JOIN tbax_marcas m on m.id_marca = p.id_marca
where a.id_producto BETWEEN 71 and 88 and i.id_estado_instalacion in (19,20)
GROUP by ci.id_ciudad, p.id_producto
ORDER by ci.id_ciudad,m.id_marca, p.id_producto
这是结果
但我需要这样的东西:
您想从分组依据子句中删除城市。因此,条件表达式需要在聚合函数中:
SELECT p.producto,
AVG(case when ci.id_ciudad = 1 then a.valor end) as medellin,
AVG(case when ci.id_ciudad = 126 then a.valor end as barranquilla,
...
FROM tbma_productos p
LEFT JOIN tbma_activacion_producto a on p.id_producto = a.id_producto
LEFT JOIN tbma_activaciones i on i.id_instalacion = a.id_activacion
LEFT JOIN tbax_almacenes c on c.id_almacen = i.id_punto_venta
LEFT JOIN tbax_ciudades ci on ci.id_ciudad = c.id_ciudad
LEFT JOIN tbax_marcas m on m.id_marca = p.id_marca
where a.id_producto BETWEEN 71 and 88 and i.id_estado_instalacion in (19,20)
GROUP BY p.id_producto
ORDER BY p.id_producto
我不知道你是否要在 group by
子句中使用 m.marca
。它没有显示在您想要的结果中,所以我删除了它。
我连接了几个表来进行查询,City、Product、Marca 和 Sales,我需要按产品分组并按每个城市的销售额显示 de average
SELECT m.marca, p.producto,
case when ci.id_ciudad = 1 then AVG(a.valor) end as medellin,
case when ci.id_ciudad = 126 then AVG(a.valor) end as barranquilla,
case when ci.id_ciudad = 149 then AVG(a.valor) end as bogota,
case when ci.id_ciudad = 150 then AVG(a.valor) end as cartagena,
case when ci.id_ciudad = 1004 then AVG(a.valor) end as cali
FROM tbma_productos p
LEFT JOIN tbma_activacion_producto a on p.id_producto = a.id_producto
LEFT JOIN tbma_activaciones i on i.id_instalacion = a.id_activacion
LEFT JOIN tbax_almacenes c on c.id_almacen = i.id_punto_venta
LEFT JOIN tbax_ciudades ci on ci.id_ciudad = c.id_ciudad
LEFT JOIN tbax_marcas m on m.id_marca = p.id_marca
where a.id_producto BETWEEN 71 and 88 and i.id_estado_instalacion in (19,20)
GROUP by ci.id_ciudad, p.id_producto
ORDER by ci.id_ciudad,m.id_marca, p.id_producto
这是结果
但我需要这样的东西:
您想从分组依据子句中删除城市。因此,条件表达式需要在聚合函数中:
SELECT p.producto,
AVG(case when ci.id_ciudad = 1 then a.valor end) as medellin,
AVG(case when ci.id_ciudad = 126 then a.valor end as barranquilla,
...
FROM tbma_productos p
LEFT JOIN tbma_activacion_producto a on p.id_producto = a.id_producto
LEFT JOIN tbma_activaciones i on i.id_instalacion = a.id_activacion
LEFT JOIN tbax_almacenes c on c.id_almacen = i.id_punto_venta
LEFT JOIN tbax_ciudades ci on ci.id_ciudad = c.id_ciudad
LEFT JOIN tbax_marcas m on m.id_marca = p.id_marca
where a.id_producto BETWEEN 71 and 88 and i.id_estado_instalacion in (19,20)
GROUP BY p.id_producto
ORDER BY p.id_producto
我不知道你是否要在 group by
子句中使用 m.marca
。它没有显示在您想要的结果中,所以我删除了它。