duansen6750 2015-10-13 15:02
浏览 53
已采纳

SQL Query的分组不正确

I need to create an SQL Query that will use the following data:

shift_id, emp_id,   date,    starttime, endtime
   1    ,  55   , 2015-10-14, 06:00:00, 10:00:00
   2    ,  55   , 2015-10-15, 03:00:00, 13:00:00
   3    ,  52   , 2015-10-15, 07:00:00, 14:00:00

Then re-arrange it so that it shows the 'date' on top as a column with the starting day being the NEXT Sunday. I have begun an attempt below but if there are 2 employees (emp_id) working on the same day, it does not display grouped:

SELECT concat(firstname,' ', surname) AS 'Fname', 
    (CASE WHEN DAYOFWEEK(date) = 1 THEN concat(starttime,'-', endtime) END) `Sunday`,
    (CASE WHEN DAYOFWEEK(date) = 2 THEN concat(starttime,'-', endtime) END) `Monday`,
    (CASE WHEN DAYOFWEEK(date) = 3 THEN concat(starttime,'-', endtime)  END) `Tuesday`,
    (CASE WHEN DAYOFWEEK(date) = 4 THEN concat(starttime,'-', endtime)  END) `Wednesday`,
    (CASE WHEN DAYOFWEEK(date) = 5 THEN concat(starttime,'-', endtime)  END) `Thursday`,
    (CASE WHEN DAYOFWEEK(date) = 6 THEN concat(starttime,'-', endtime)  END) `Friday`,
    (CASE WHEN DAYOFWEEK(date) = 7 THEN concat(starttime,'-', endtime)  END) `Saturday`
FROM shifts NATURAL JOIN employees
GROUP BY Fname, date

Here is the result with showing the employee name twice instead of grouping? Where have I gone wrong?:

Result

Using GROUP_CONCAT, the incorrect result shows as below:

Result2

  • 写回答

2条回答 默认 最新

  • duanfenhui5511 2015-10-13 16:15
    关注

    You are selecting more columns than you've specified in your GROUP BY. As a result, MySQL will simply pick the data from one record in that group to represent it: https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html

    MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate.

    Try something like this:

    SELECT concat(firstname,' ', surname) AS 'Fname', 
        MAX(CASE WHEN DAYOFWEEK(date) = 1 THEN concat(starttime,'-', endtime) END) `Sunday`,
        MAX(CASE WHEN DAYOFWEEK(date) = 2 THEN concat(starttime,'-', endtime) END) `Monday`,
        MAX(CASE WHEN DAYOFWEEK(date) = 3 THEN concat(starttime,'-', endtime)  END) `Tuesday`,
        MAX(CASE WHEN DAYOFWEEK(date) = 4 THEN concat(starttime,'-', endtime)  END) `Wednesday`,
        MAX(CASE WHEN DAYOFWEEK(date) = 5 THEN concat(starttime,'-', endtime)  END) `Thursday`,
        MAX(CASE WHEN DAYOFWEEK(date) = 6 THEN concat(starttime,'-', endtime)  END) `Friday`,
        MAX(CASE WHEN DAYOFWEEK(date) = 7 THEN concat(starttime,'-', endtime)  END) `Saturday`
    FROM shifts NATURAL JOIN employees
    GROUP BY Fname, date
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP