2011-11-11 101 views
0

我是tsql的新手,並使用mysql db。看一個示例代碼(在下面),從數據庫中獲取的數據,每2學時】每15分鐘獲取一次avg數據

SELECT date(`dateTime`) dateDay, 2*floor(date_format(`dateTime`,'%H')/2) dateHour, 
     avg(channel1), avg(channel2), avg(channel3) 
FROM `Table` 
WHERE `id` =1 
AND `dateTime` >= '2011-10-15 00:00:01' 
AND `dateTime` <= '2011-10-17 23:59:59' 

然後我做了基於上面的代碼來獲得數據每15分鐘幾點變化:

SELECT date(`dateTime`) dateDay, 15*floor(date_format(`dateTime`,'%i')/15) dateHour, 
     avg(channel1), avg(channel2), avg(channel3) 
FROM `Table` 
WHERE `id` =1 
AND `dateTime` >= '2011-10-15 00:00:01' 
AND `dateTime` <= '2011-10-17 23:59:59' 

group by date(`dateTime`), 15*floor(date_format(`dateTime`,'%i')/15) 

然而,查詢不正確。

我的問題是:

  1. 如何修改查詢來獲取數據每15分鐘?
  2. 如何編寫sql查詢以獲取所有數據? dateDay和dateHour列需要在這裏。
+0

[Group mysql query by 15 min intervals]可能重複(http://stackoverflow.com/questions/2793994/group-mysql-query-by-15-min-intervals) –

回答

0

1:每15分鐘:

SELECT日期(dateTime)dateDay,2 *地板(DATE_FORMAT(dateTime, '%H')/ 2)dateHour, AVG(通道1),AVG(通道2 ),AVG(通道3)

- >

SELECT date(`dateTime`) dateDay 
    ,CONCAT(2*floor(date_format(`dateTime`,'%H')/2), ':', 15*FLOOR(DATE_FORMAT(`dateTime`, '%i')/15)) dateQuarter 
    ,avg(channel1), avg(channel2), avg(channel3) 
    FROM `Table` 
    WHERE `id` =1 
    AND `dateTime` >= '2011-10-15 00:00:01' 
    AND `dateTime` <= '2011-10-17 23:59:59' 
    GROUP BY 1,2; 

2:所有數據。我不確定你想要什麼。但是這裏的每一行都有小時:分鐘在dateHour字段中。

SELECT date(`dateTime`) dateDay, 
     ,DATE_FORMAT(`dateTime`, '%H:%i') dateHour, 
FROM `Table` 
WHERE `id` =1 
    AND `dateTime` >= '2011-10-15 00:00:01' 
    AND `dateTime` <= '2011-10-17 23:59:59' 
相關問題