Group records by time

后端 未结 2 879
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 07:44

I have a table containing a datetime column and some misc other columns. The datetime column represents an event happening. It can either contains a time (event happened at

相关标签:
2条回答
  • 2020-12-18 08:11

    Give this a try:

    select datetime((strftime('%s', time) / 900) * 900, 'unixepoch') interval,
           count(*) cnt
    from t
    group by interval
    order by interval
    

    Check the fiddle here.

    0 讨论(0)
  • 2020-12-18 08:32

    I have limited SQLite background (and no practice instance), but I'd try grabbing the minutes using

    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
    

    with the %M modifier (http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html)

    Then divide that by 15 and get the FLOOR of your quotient to figure out which quarter-hour you're in (e.g., 0, 1, 2, or 3)

    cast(x as int)
    

    Getting the floor value of a number in SQLite?

    Strung together it might look something like:

    Select cast( (strftime( 'YYYY-MM-DD HH:MI:SS', your_time_field, '%M') / 15) as int) from your_table
    

    (you might need to cast before you divide by 15 as well, since strftime probably returns a string)

    Then group by the quarter-hour.

    Sorry I don't have exact syntax for you, but that approach should enable you to get the functional groupings, after which you can massage the output to make it look how you want.

    0 讨论(0)
提交回复
热议问题