duanshan1511 2019-03-21 21:45
浏览 165
已采纳

如何计算两个时间戳之间的差异,然后逐个月地在SQL中进行图表绘制

I'm trying to form a SQL statement that meets the following criteria based on records that have date/timestamps and various other varchar and int fields.

  • Calculate the difference between two fields named time_depart and time_return in decimal hours
  • Total up the differences and group by month up to the last 12 months.
  • Separate totals based on event_type

Example Data Set

table events:

id | time_depart         | time_return         | event_type  | summary
--------------------------------------------------------------
1  | 2019-02-11 10:00:00 | 2019-02-11 10:30:00 | 1           | test summary
2  | 2019-02-11 10:30:00 | 2019-02-11 11:30:00 | 1           | some more data
3  | 2019-02-11 11:00:00 | 2019-02-11 12:30:00 | 2           | even more data
4  | 2019-02-11 11:30:00 | 2019-02-11 13:30:00 | 2           | just a summary
5  | 2019-02-11 12:00:00 | 2019-02-11 14:30:00 | 2           | again more
......

Would like a return similar to where event_type = 1 is 'training' and event_type = 2 is a 'mission'

month     | trainingTime | missionTime  
--------------------------------------------------------------
january   | 15.25        | 22 
february  | 20           | 25
march     | 10.5         | 35.5
april     | 52           | 20 
may       | 64           | 72 
june      | 100          | 10.75 
july      | 45           | 0 
august    | 26           | 15
september | 10.5         | 65
october   | 55           | 8
november  | 44           | 12.25 
december  | 17           | 0
  • 写回答

3条回答 默认 最新

  • dtuzjzs3853 2019-03-21 22:26
    关注

    A typical solution is to use conditional aggregation. Group data by month, then compute separatly the sum of each event type.

    It is unclear in which format you expect the durations to be expressed. The following query will return durations in seconds (you can then format them in your application layer or using MySQL date and time functions).

    Query:

    SELECT 
        DATE_FORMAT(time_depart, '%Y-%M') eventMonth,
        SUM(
            CASE WHEN event_type = 1 
            THEN TIMESTAMPDIFF(SECOND, time_depart, time_return)
            ELSE 0 
        END) trainingTime,
        SUM(
            CASE WHEN event_type = 2 
            THEN TIMESTAMPDIFF(SECOND, time_depart, time_return)
            ELSE 0 
        END) missionTime
    FROM events
    GROUP BY DATE_FORMAT(time_depart, '%Y-%M')
    ORDER BY eventMonth
    

    NB: it is probably a good idea to use the year as a grouping condition too, in case your data spreads over more than 12 months.

    Demo on DB Fiddle with your sample data:

    | eventMonth    | trainingTime | missionTime |
    | ------------- | ------------ | ----------- |
    | 2019-February | 5400         | 21600       |
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动