存储在数据库中的平均操作时间

时间:2017-01-03 11:34:51

标签: mysql sql

我需要计算存储在数据库中的所有操作的平均时间。我存储操作的表格如下:

 creation time       | operation_type | operation_id
 2017-01-03 11:14:25 | START          | 1
 2017-01-03 11:14:26 | START          | 2
 2017-01-03 11:14:28 | END            | 2
 2017-01-03 11:14:30 | END            | 1

在这种情况下,操作1需要5秒钟,操作2需要2秒才能完成。

如何计算MySQL中这些操作的平均值?

编辑: 似乎operation_id不需要是唯一的 - 给定的操作可能会执行多次,因此表可能如下所示:

 creation time       | operation_type | operation_id
 2017-01-03 11:14:25 | START          | 1
 2017-01-03 11:14:26 | START          | 2
 2017-01-03 11:14:28 | END            | 2
 2017-01-03 11:14:30 | END            | 1
 2017-01-03 11:15:00 | START          | 1
 2017-01-03 11:15:10 | END            | 1

我应该在查询中添加什么来正确计算所有这些操作的平均时间?

3 个答案:

答案 0 :(得分:2)

由于操作的END始终在START之后,因此可以使用MIN和MAX

select avg(diff)
from
(
      select operation_id, 
             TIME_TO_SEC(TIMEDIFF(max(creation_time), min(creation_time))) as diff
      from your_table
      group by operation_id
) tmp

答案 1 :(得分:2)

我不确定子查询是否必要......

<header class="headerpart" style="z-index:9999;position:relative;">
        <div class="container">
            <div class="logo">
                <a href="#">

                    <img class="abc" src="images-Logo.png" alt="krishnamohan" height="50" width="235" />
                </a>
            </div>
            <div class="menu" >
                <nav>
                    <ul>
                        <li><a href="#">Home</a></li>
                        <li>
                            <a href="#">Courses</a>
                            <ul>
                                <li><a href="#">Lesson</a></li>
                                <li>
                                    <a href="#">
                                        Practicals
                                    </a>
                                </li>
                                <li><a href="#">Projects</a></li>
                            </ul>
                        </li>
                        <li><a href="#">Contact</a></li>
                        <li>
                            <a href="url">login</a>

                        </li>
                        <li><a href="url">Sign up</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </header>
<div id="jssor_1" style="position: relative; margin: 0 auto; top: 0px; left: 0px; width: 1300px; height: 500px; overflow: hidden; visibility: hidden;">
        <!-- Loading Screen -->
        <div data-u="loading" style="position: absolute; top: 0px; left: 0px;">
            <div style="filter: alpha(opacity=70); opacity: 0.7; position: absolute; display: block; top: 0px; left: 0px; width: 100%; height: 100%;"></div>
            <div style="position:absolute;display:block;background:url('img/loading.gif') no-repeat center center;top:0px;left:0px;width:100%;height:100%;"></div>
        </div>
        <div data-u="slides" style="cursor: default; position: relative; top: 0px; left: 0px; width: 1300px; height: 500px; overflow: hidden;">
            <div data-p="225.00">
                <img data-u="image" src="img/red.jpg" />
                <div style="position:absolute;top:30px;left:30px;width:480px;height:120px;z-index:0;font-size:50px;color:#ffffff;line-height:60px;">TOUCH SWIPE SLIDER</div>
                <div style="position:absolute;top:300px;left:30px;width:480px;height:120px;z-index:0;font-size:30px;color:#ffffff;line-height:38px;">Build your slider with anything, includes image, content, text, html, photo, picture</div>
                <div data-u="caption" data-t="0" style="position:absolute;top:120px;left:650px;width:470px;height:220px;z-index:0;">
                    <img style="position:absolute;top:0px;left:0px;width:470px;height:220px;z-index:0;" src="img/c-phone-horizontal.png" />
                    <div style="position:absolute;top:4px;left:45px;width:379px;height:213px;z-index:0; overflow: hidden;">
                        <img data-u="caption" data-t="1" style="position:absolute;top:0px;left:0px;width:379px;height:213px;z-index:0;" src="img/c-slide-1.jpg" />
                        <img data-u="caption" data-t="2" style="position:absolute;top:0px;left:379px;width:379px;height:213px;z-index:0;" src="img/c-slide-3.jpg" />
                    </div>
                    <img style="position:absolute;top:4px;left:45px;width:379px;height:213px;z-index:0;" src="img/c-navigator-horizontal.png" />
                    <img data-u="caption" data-t="3" style="position:absolute;top:740px;left:1600px;width:257px;height:300px;z-index:0;" src="img/c-finger-pointing.png" />
                </div>
            </div>
            <div data-p="225.00" style="display: none;">
                <img data-u="image" src="img/purple.jpg" />
            </div>
            <div data-p="225.00" data-po="80% 55%" style="display: none;">
                <img data-u="image" src="img/blue.jpg" />
            </div>
            <a data-u="any" href="" style="display:none">Full Width Slider</a>
        </div>
        <!-- Bullet Navigator -->
        <div data-u="navigator" class="jssorb05" style="bottom:16px;right:16px;" data-autocenter="1">
            <!-- bullet navigator item prototype -->
            <div data-u="prototype" style="width:16px;height:16px;"></div>
        </div>
        <!-- Arrow Navigator -->
        <span data-u="arrowleft" class="jssora22l" style="top:0px;left:8px;width:40px;height:58px;" data-autocenter="2"></span>
        <span data-u="arrowright" class="jssora22r" style="top:0px;right:8px;width:40px;height:58px;" data-autocenter="2"></span>
    </div>

答案 2 :(得分:1)

select avg(diff)
from
(
select a1.operation_id, timediff(a2.operation_time, a1.operation_time) as diff
from oper a1 -- No table name provided, went with 'oper' because it made sense in my head
inner join oper a2
  on a1.operation_id = a2.operation_id
where a1.operation_type = 'START'
and a2.operation_type = 'END'
)