2013-06-23 56 views
0

我有這個創建樞軸的MySQL的SELECT選擇。拆分複雜的mysql按日期選擇

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; 

檢查這個FIDDLE:SELECT

的問題是,我需要按日期sepereted的數據,目前,我讓他們都在一排:

2013年6月19日09 :09:01,19.06.2013 10:29:28,19.06.2013 14:01:00 11,20.06.2013 10:01:00

希望你能幫我解決這個問題。如果可能,請使用SQL小提琴

+0

你能澄清你所說的「按日期分離」是什麼意思?我看了你的SQLFiddle輸出。你能展示你在找什麼嗎? –

回答

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;