简体   繁体   中英

Incorrect grouping 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?:

结果

Using GROUP_CONCAT, the incorrect result shows as below:

结果2

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

Is this what you looking for ? if not, please make a sample result. The first SELECT is only to hide some tmp fields. If you get the result with a program you can remove it

SELECT Fname, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
FROM (
  SELECT 
      @timestr:=  concat(starttime,'-', endtime) AS a,
      @datenr:= DAYOFWEEK(DATE) AS b,
      concat(firstname,' ', surname) AS 'Fname',
      COALESCE( IF(@datenr  = 1, @timestr,''))  AS `Sunday`,
      COALESCE( IF(@datenr  = 2, @timestr,''))  AS `Monday`,
      COALESCE( IF(@datenr  = 3, @timestr,''))  AS `Tuesday`,
      COALESCE( IF(@datenr  = 4, @timestr,''))  AS `Wednesday`,
      COALESCE( IF(@datenr  = 5, @timestr,''))  AS `Thursday`,
      COALESCE( IF(@datenr  = 6, @timestr,''))  AS `Friday`,
      COALESCE( IF(@datenr  = 7, @timestr,''))  AS `Saturday`
  FROM shifts s
  LEFT JOIN employees e ON e.emp_id = s.emp_id
  GROUP BY s.date, s.emp_id
) AS result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM