【问题标题】:Removing duplicate results CASE - UNION SELECT - Mysql删除重复结果 CASE - UNION SELECT - Mysql
【发布时间】: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     |        |
+--------------+-------+--------+-----------+--------+---------+--------+

【问题讨论】:

    标签: mysql case union


    【解决方案1】:

    您需要按tipo_formato 分组,并为给定的tipo_formato 值总结一周中每一天的计数。

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

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

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

    这个查询应该适合你:

    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
    

    【讨论】:

    • 非常感谢 Ike Walker,您的帮助真的很有用,不仅如此,它还大大减少了代码,优化并且更容易理解,很高兴收到这样的帮助。我已经验证了代码,它运行良好。
    猜你喜欢
    • 2011-11-30
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2013-02-18
    • 2020-07-21
    • 2011-12-09
    相关资源
    最近更新 更多