0

我有一个创建数据透视表的 MySQL SELECT。

SELECT t.*,
   sum(HOUR(TIMEDIFF(STR_TO_DATE(Arbeitsbeginn, '%d.%m.%Y %H:%i:%s'),
   STR_TO_DATE(Arbeitsende, '%d.%m.%Y %H:%i:%s')))) AS ARBEITSZEIT,
   sum(HOUR(TIMEDIFF(STR_TO_DATE(Projektbeginn, '%d.%m.%Y %H:%i:%s'), 
                     STR_TO_DATE(Projektende, '%d.%m.%Y %H:%i:%s')))) AS Projektzeit
FROM (SELECT DriverName,
         Baustelle,
         Arbeitsbeginn,
         Arbeitsende,
         Projektbeginn,
         Projektende
  FROM (SELECT DRIVERNAME,
               GROUP_CONCAT(IF(ACTIONTEXT = 'PB', AREA, NULL)) AS 'Baustelle',
               GROUP_CONCAT(IF(ACTIONTEXT = 'Arbeitsbeginn', DATETIME, NULL)) AS 'Arbeitsbeginn',
               GROUP_CONCAT(IF(ACTIONTEXT = 'Arbeitsende', DATETIME, NULL)) AS 'Arbeitsende',
               GROUP_CONCAT(IF(ACTIONTEXT = 'PB', DATETIME, NULL)) AS 'Projektbeginn',
               GROUP_CONCAT(IF(ACTIONTEXT = 'PE', DATETIME, NULL)) AS 'Projektende'
        FROM geoImportRoot
        GROUP BY DRIVERNAME
       ) A CROSS JOIN
       (SELECT 1 AS n UNION ALL SELECT 2) n
) t
GROUP BY DriverName;

检查这个小提琴:选择

问题是,我需要按日期分隔的数据,此时我将它们全部连续获取:

19.06.2013 09:09:01,19.06.2013 10:29:28,19.06.2013 14:01:11,20.06.2013 10:01:00

希望你能帮我解决这个问题。如果可能,请使用 SQL Fiddle

4

1 回答 1

1

您不应将日期/时间值存储为字符串。为此,您应该使用内置数据类型。

在任何情况下,数据都是字符串的前 10 个字符。以下变体按天拆分数据:

SELECT t.*,
  sum(HOUR(TIMEDIFF(STR_TO_DATE(Arbeitsbeginn, '%d.%m.%Y %H:%i:%s'), STR_TO_DATE(Arbeitsende, '%d.%m.%Y %H:%i:%s')))) AS ARBEITSZEIT,
  sum(HOUR(TIMEDIFF(STR_TO_DATE(Projektbeginn, '%d.%m.%Y %H:%i:%s'), STR_TO_DATE(Projektende, '%d.%m.%Y %H:%i:%s')))) AS Projektzeit
FROM (SELECT DriverName, thedate,
             Baustelle,
             Arbeitsbeginn,
             Arbeitsende,
             Projektbeginn,
             Projektende

      FROM (SELECT DRIVERNAME, left(`datetime`, 10) as thedate,
                   GROUP_CONCAT(IF(ACTIONTEXT = 'PB', AREA, NULL)) AS 'Baustelle',
                   GROUP_CONCAT(IF(ACTIONTEXT = 'Arbeitsbeginn', DATETIME, NULL)) AS 'Arbeitsbeginn',
                   GROUP_CONCAT(IF(ACTIONTEXT = 'Arbeitsende', DATETIME, NULL)) AS 'Arbeitsende',
                   GROUP_CONCAT(IF(ACTIONTEXT = 'PB', DATETIME, NULL)) AS 'Projektbeginn',
                   GROUP_CONCAT(IF(ACTIONTEXT = 'PE', DATETIME, NULL)) AS 'Projektende'
            FROM geoImportRoot
            GROUP BY DRIVERNAME, left(`datetime`, 12)
           ) A CROSS JOIN
           (SELECT 1 AS n UNION ALL SELECT 2) n
    ) t
GROUP BY DriverName, thedate;
于 2013-06-23T20:04:44.127 回答