gpt4 book ai didi

mysql - 删除重复结果 CASE - UNION SELECT - Mysql

转载 作者:行者123 更新时间:2023-11-30 21:51:19 25 4
gpt4 key购买 nike

我有重复结果的问题,我有一个很大的查询。

我正在尝试进行查询,其中产品类型在一周内显示不同的金额,我遇到的问题是当产品应该出现在同一条记录中的两天时,它是重复的,正如我在下面显示的那样可以帮助我,我开始让我发疯。

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 | |
+--------------+-------+--------+-----------+--------+---------+--------+

最佳答案

您需要按 tipo_formato 分组,并根据给定的 tipo_formato 值对一周中每一天的计数求和。

您当前的查询还增加了一些不必要的复杂性。您可以去掉子查询,只需更改 CASE 语句以打开 weekday(pedidos.fecha_entrega)

我还建议您启用 ONLY_FULL_GROUP_BY sql_mode 以避免 SELECT 子句与 GROUP BY 子句不匹配的问题。您可以阅读更多相关信息 here .

我不确定你的表有多大,但你也可以通过索引来提高yearweek(pedidos.fecha_entrega,1) = yearweek(now())的性能>pedidos.fecha_entrega 列并重写查询以避免在函数调用中包装 pedidos.fecha_entrega。那不是你真正想要的重新询问,所以我不会触及查询的那部分,但值得考虑表是否会很大。

这个查询应该适合你:

SELECT formatopeso.tipo_formato,
SUM(CASE
when weekday(pedidos.fecha_entrega)= 0
then pedidos.unidades
else NULL
END) as Lunes,
SUM(CASE
when weekday(pedidos.fecha_entrega)= 1
then pedidos.unidades
else NULL
END) as Martes,
SUM(CASE
when weekday(pedidos.fecha_entrega)= 2
then pedidos.unidades
else NULL
END) as Miércoles,
SUM(CASE
when weekday(pedidos.fecha_entrega)= 3
then pedidos.unidades
else NULL
END) as Jueves,
SUM(CASE
when weekday(pedidos.fecha_entrega)= 4
then pedidos.unidades
else NULL
END) as Viernes,
SUM(CASE
when weekday(pedidos.fecha_entrega)= 5
then pedidos.unidades
else NULL
END) as Sábado
FROM pedidos
INNER JOIN formatopeso
ON formatopeso.id_formatopeso = pedidos.id_formatopeso
where yearweek(pedidos.fecha_entrega,1) = yearweek(now())
GROUP BY formatopeso.tipo_formato

关于mysql - 删除重复结果 CASE - UNION SELECT - Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47146392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com