【发布时间】:2018-04-19 04:10:07
【问题描述】:
我遇到了重复结果的问题,我有一个很大的查询。
我正在尝试查询产品类型在一周内显示不同数量的情况,我遇到的问题是当产品应该在同一记录中出现两天时重复,正如我在某人下方显示的那样可以帮助我,我开始让我发疯了。
SELECT formatopeso.tipo_formato,
CASE
when dia = '1'
then sumatotal
else ''
END as Lunes,
CASE
when dia = '2'
then sumatotal
else ''
END as Martes,
CASE
when dia = '3'
then sumatotal
else ''
END as Miércoles,
CASE
when dia = '4'
then sumatotal
else ''
END as Jueves,
CASE
when dia = '5'
then sumatotal
else ''
END as Viernes,
CASE
when dia = '6'
then sumatotal
else ''
END as Sábado
FROM (
select '1' AS dia, pedidos.fecha_entrega, id_formatopeso, sum(pedidos.unidades) as sumatotal
from pedidos
where yearweek(fecha_entrega,1) = yearweek(now()) and weekday(fecha_entrega)= 0
UNION
select '2' as dia, pedidos.fecha_entrega, id_formatopeso, sum(pedidos.unidades) as sumatotal
from pedidos
where yearweek(fecha_entrega,1) = yearweek(now()) and weekday(fecha_entrega)= 1
UNION
select '3' as dia, pedidos.fecha_entrega, id_formatopeso, sum(pedidos.unidades) as sumatotal
from pedidos
where yearweek(fecha_entrega,1) = yearweek(now()) and weekday(fecha_entrega)= 2
UNION
select '4' as dia, pedidos.fecha_entrega, id_formatopeso, sum(pedidos.unidades) as sumatotal
from pedidos
where yearweek(fecha_entrega,1) = yearweek(now()) and weekday(fecha_entrega)= 3
UNION
select '5' as dia, pedidos.fecha_entrega, id_formatopeso, sum(pedidos.unidades) as sumatotal
from pedidos
where yearweek(fecha_entrega,1) = yearweek(now()) and weekday(fecha_entrega)= 4
UNION
select '6' as dia, pedidos.fecha_entrega, id_formatopeso, sum(pedidos.unidades) as sumatotal
from pedidos
where yearweek(fecha_entrega,1) = yearweek(now()) and weekday(fecha_entrega)= 5)
pedidos INNER JOIN formatopeso
ON formatopeso.id_formatopeso = pedidos.id_formatopeso
GROUP BY pedidos.id_formatopeso, fecha_entrega;
我有这个结果:
+--------------+-------+--------+-----------+--------+---------+--------+
| tipo_formato | Lunes | Martes | Miércoles | Jueves | Viernes | Sábado |
+--------------+-------+--------+-----------+--------+---------+--------+
| 22Ø 180gr | | 450 | | | | |
| 22Ø 180gr | | | 100 | | | |
| 27Ø 270gr | | | | 200 | | |
| 27Ø 270gr | | | | | 300 | |
+--------------+-------+--------+-----------+--------+---------+--------+
我需要这样的结果:
+--------------+-------+--------+-----------+--------+---------+--------+
| tipo_formato | Lunes | Martes | Miércoles | Jueves | Viernes | Sábado |
+--------------+-------+--------+-----------+--------+---------+--------+
| 22Ø 180gr | | 450 | 100 | | | |
| 27Ø 270gr | | | | 200 | 300 | |
+--------------+-------+--------+-----------+--------+---------+--------+
【问题讨论】: