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条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)