2015-04-14 81 views
0

我有這樣一個數據集:SQL獲得數計每分鐘

id  name  updated_at 
1  test1  2014-06-30 09:00:00 
1  test2  2014-06-30 09:01:10 
1  test3  2014-06-30 09:01:23 
1  test4  2014-06-30 09:01:43 
1  test5  2014-06-30 09:02:02 
1  test6  2014-06-30 09:02:34 
1  test7  2014-06-30 09:03:22 
1  test8  2014-06-30 09:03:28 

我需要一分來獲得行的計數的最後十分鐘。所以它應該總是返回十個數字,即最後更新的行數。任何想法如何做到這一點和有效?

+0

http://stackoverflow.com/a/13761926/2589202 – paqogomez

回答

0

去年10結果

http://sqlfiddle.com/#!9/3d586/22

--get the minute component of the update time 
select minute(updated_at) as Sec 
--count the number of records which have this minute 
, count(1) as Cnt 
from myTable 
--use group by to ensure we return 1 row per minute 
group by minute(updated_at) 
--list from most recent working backwards 
order by minute(updated_at) desc 
--return up to 10 results 
limit 10 

結果最後10分鐘

http://sqlfiddle.com/#!9/3d586/26

--get the minute component of the update time 
select minute(y.d) as Min 
--count the number of records which have this minute 
--use m.id instead of 1 or * to ensure where there's no result from myTable 
--we don't count any rows 
, count(m.id) as Cnt 
from 
(
    --get the current date's minute, offset by a given amount 
    select date_add(now(), interval -x.a minute) d 
    from 
    (
     --the list of given amounts by which to offset the above date 
     select 0 a 
     union select 1 
     union select 2 
     union select 3 
     union select 4 
     union select 5 
     union select 6 
     union select 7 
     union select 8 
     union select 9 
    ) x 
) y 
--left join to ensure the above list drives which results we get, 
--regardless of whether there are matching entries in myTable 
left outer join myTable m 
--join on the minute of each date 
on minute(m.updated_at) = minute(y.d) 
--use group by to ensure we return 1 row per minute 
group by minute(y.d) 
--list from most recent working backwards 
order by minute(y.d) desc 
+0

感謝您的快速回復。還有一個問題。你的答案使用第二種方法,這是獲得最後10秒而不是分鐘?另外,如果在某一分鐘內沒有找到結果,我需要它返回零。因此,如果當前日期時間爲2014-06-30 09:05:28,則前兩行將在9:05和9:04時返回0。所以它總是會返回10個數字,即使它們都是零。 – sudopratt

+0

通常情況下,如果答案包含解釋代碼意圖做什麼的解釋,以及爲什麼解決問題而不介紹其他問題,則答案會更有幫助。 (這篇文章被至少一個用戶標記過,大概是因爲他們認爲沒有解釋的答案應該被刪除。) –

+0

是的,它已經達到了最後的十秒鐘。 –

0

由於您想插值,因此它很快變得更加複雜。最後十分鐘內每分鐘創建一個派生表格作爲一行可能是最簡單的,然後將它們留在那裏。下面是做這件事:

select minute(now() - interval m minute) 'minutes ago', count(case when t.updated_at is not null then t.updated_at end) 
from 
    (select minute(now()) m 
    union 
    select minute(now() - interval 1 minute) m 
    union 
    select minute(now() - interval 2 minute) m 
    union 
    select minute(now() - interval 3 minute) m 
    union 
    select minute(now() - interval 4 minute) m 
    union 
    select minute(now() - interval 5 minute) m 
    union 
    select minute(now() - interval 6 minute) m 
    union 
    select minute(now() - interval 7 minute) m 
    union 
    select minute(now() - interval 8 minute) m 
    union 
    select minute(now() - interval 9 minute) m 
    union 
    select minute(now() - interval 10 minute) m 
) q 
    left join myTable t 
    on q.m = minute(t.updated_at) 
     and t.updated_at >= now() - interval 10 minute 
    group by m 
    order by m desc 

這裏有一個小提琴,但你必須改變模式(剛加入某一個空格),爲了重建它來得到準確的結果:

http://sqlfiddle.com/#!9/f3320/1