2016-07-28 36 views
0

這個映像文件是樣本數據enter image description here如何獲得MySQL查詢數據每5分鐘的總和(列)和最後一個索引值

第一形象就是你的查詢

enter image description here

第二圖象是我希望這一觀點

enter image description here

時間值完全分00秒〜分00秒

我的意思是110000〜110450 這樣值「04米:50多歲」

這是檢查數據

例如這是採樣數據

數據插入每10秒。

## time money amount date 
HHmmss 
100000 500 30 2016-07-28 
------------------------ 

100010 800 740 2016-07-28 
------------------------- 

100450 300 80 2016-07-28 
------------------------ 

100500 200 5 2016-07-28 
----------------------- 

100510 900 9144 2016-07-28 
-------------------------- 

~~~~~~~~~~~~~~~~~~~~~~~~ 

110000 500 5 2016-07-28 
----------------------- 

110500 233 5 2016-07-28 
======================= 

我想獲得每5分鐘數據之和視圖

這種查詢是每1分鐘數據

如何改變容易喲使用

select sum(amount), 
     lpad(cast(time - 5 as char),6,'0') AS dateVal, 
     substr(lpad(cast(time - 5 as char),6,'0'),1,4), 
     date, 
     max(time) 
from example 
where date = '2016-07-28' 
group by substr(dateVal, 1, 4) 
order by dateVal asc; 

,我希望得到這樣一個數據這

amountsum time(range)   money 
888   100000 ~100450  i want last insert money value 44200 
95   100500 ~100950  i want last insert money value 44200 
22   101000 ~101450  i want last insert money value 44200 
6843  101500 ~101950  i want last insert money value 44200 
2213  102000 ~102450  i want last insert money value 44200 


sequence time money mpare sel buy amount date 
1875 082100 710,000 0 710,000 702,000 27 2016-07-29 
1874 082710 710,000 0 710,000 702,000 22 2016-07-29 
1873 082730 710,000 0 710,000 702,000 1 2016-07-29 
1877 090030 710,000 0 711,000 710,000 848 2016-07-29 
1876 090100 711,000 1,000 711,000 710,000 2 2016-07-29 
1884 090110 711,000 1,000 711,000 710,000 66 2016-07-29 
1883 090120 710,000 0 711,000 710,000 5 2016-07-29 
1882 090130 711,000 1,000 711,000 710,000 53 2016-07-29 
1881 090140 710,000 0 711,000 710,000 11 2016-07-29 
1880 090150 710,000 0 711,000 710,000 2 2016-07-29 
1879 090200 710,000 0 711,000 710,000 10 2016-07-29 
1878 090210 710,000 0 711,000 710,000 10 2016-07-29 
1889 090220 712,000 2,000 712,000 711,000 313 2016-07-29 
1888 090230 712,000 2,000 712,000 711,000 21 2016-07-29 
1887 090240 712,000 2,000 712,000 711,000 4 2016-07-29 
1886 090250 712,000 2,000 712,000 711,000 2 2016-07-29 
1885 090300 713,000 3,000 713,000 712,000 25 2016-07-29 
1894 090310 713,000 3,000 714,000 713,000 13 2016-07-29 
1893 090330 714,000 4,000 714,000 713,000 13 2016-07-29 
1892 090350 713,000 3,000 714,000 713,000 3 2016-07-29 
1891 090400 714,000 4,000 714,000 713,000 1 2016-07-29 
1890 090410 714,000 4,000 714,000 713,000 49 2016-07-29 
1900 090420 715,000 5,000 715,000 714,000 27 2016-07-29 
1899 090430 716,000 6,000 716,000 715,000 46 2016-07-29 
1898 090440 716,000 6,000 716,000 715,000 86 2016-07-29 
1897 090450 716,000 6,000 716,000 715,000 59 2016-07-29 
1896 090500 716,000 6,000 716,000 715,000 3 2016-07-29 
1895 090510 716,000 6,000 716,000 715,000 2 2016-07-29 
1906 090520 717,000 7,000 717,000 716,000 15 2016-07-29 
1905 090530 717,000 7,000 717,000 716,000 31 2016-07-29 
1904 090540 717,000 7,000 717,000 716,000 3 2016-07-29 
1903 090550 716,000 6,000 717,000 716,000 77 2016-07-29 
+0

什麼是你想要的結果? – Blank

回答

0

不知道是什麼導致你究竟想要的,但試試這個:

select 
    sum(amount) as amountsum, 
    concat(min(`time`), '~', max(`time`)) as `time(range)`, 
    `date` 
from example 
cross join (
    select min(`time`) as minTime from example where `date` = '2016-07-28' 
) t 
where `date` = '2016-07-28' 
group by truncate((time_to_sec(`time`) - time_to_sec(`minTime`))/60/5, 0) 
order by max(`time`) asc; 

Demo Here

+0

請檢查我的問題和價值 –

+0

請再檢查一次 –

+0

@ joshua.kim如果僅僅爲您爲我們舉個例子的數據,結果是什麼樣的?雖然你給我關於你想要的結果的圖像,但我無法找到我的查詢中哪裏出錯。 :-( – Blank

相關問題