MySQL - 最后一天插入的平均百分比值

标签 mysql sql database

我在 MySQL 中有一个表:

+-----------------+-------------+------+-----+---------------------+-------+
| Field           | Type        | Null | Key | Default             | Extra |
+-----------------+-------------+------+-----+---------------------+-------+
| period_duration | datetime    | NO   | PRI | 0000-00-00 00:00:00 |       |
| duration        | varchar(6)  | YES  |     | NULL                |       |
| sample          | varchar(2)  | YES  |     | NULL                |       |
| corner          | varchar(10) | YES  |     | NULL                |       |
| country         | varchar(60) | NO   | PRI |                     |       |
| roaming_partner | varchar(60) | NO   | PRI |                     |       |
| pdp_in_total    | int(8)      | YES  |     | NULL                |       |
| pdp_in_ok       | int(8)      | YES  |     | NULL                |       |
| pdp_in_not_ok   | int(8)      | YES  |     | NULL                |       |
| pdp_in_ok_rate  | int(8)      | YES  |     | NULL                |       |
+-----------------+-------------+------+-----+---------------------+-------+

这个表每小时都​​有新的插入,这使得每个 roaming_partner 有许多不同的 period_duration:

+---------------------+----------+--------+----------+---------+-----------------------+--------------+-----------+---------------+----------------+
| period_duration     | duration | sample | corner   | country | roaming_partner       | pdp_in_total | pdp_in_ok | pdp_in_not_ok | pdp_in_ok_rate |
+---------------------+----------+--------+----------+---------+-----------------------+--------------+-----------+---------------+----------------+
| 2014-12-16 14:00:00 | 3600     | 1      | GPRS_OUT | USA     | Operator1             |          796 |       787 |             9 |             99 |
| 2014-12-16 15:00:00 | 3600     | 1      | GPRS_OUT | USA     | Operator1             |         1748 |      1706 |            42 |             98 |
| 2014-12-16 16:00:00 | 3600     | 1      | GPRS_OUT | USA     | Operator1             |            7 |         7 |             0 |            100 |

“ok_rate”是百分比。

我需要创建一个 SELECT 来显示每个 countryroaming_partnerpdp_in_ok_rate 24 次插入,这些插入的平均 pdp_in_ok_rate%。

就像我希望我的 SQL 查询说: “这是你表中每个运算符(operator)在最后 24 次插入中的平均 pdp_in_ok_rate。不是在整个表中,而是在最后这些中。”

有人可以帮帮我吗?

最佳答案

这是否符合您的要求?

select country, roaming_partner, avg(pd_in_ok_rate) as avgrate
from table t
where period_duration >= date_sub(now(), interval -1 day)
group by country, roaming_partner;

这不会为您提供平均细节。如果您还需要详细信息,可以将其用作子查询

select t.*, cr.avgrate
from table t join
     (select country, roaming_partner, avg(pd_in_ok_rate) as avgrate
      from table t
      where period_duration >= date_sub(now(), interval -1 day)
      group by country, roaming_partner
     ) cr
     on t.country = cr.country and t.roaming_partner = cr.roaming_partner
where period_duration >= date_sub(now(), interval -1 day);

关于MySQL - 最后一天插入的平均百分比值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27512889/

相关文章:

mysql - 如何从 MYSQL 中的一张表更新另一张表?具有额外值(value)

mysql - 如何创建一个空 View (或数组)并稍后在其中插入数据?

sql - 如何在 Oracle 10+ 中对包含 NULL 的列使用基于函数的索引?

mysql - 如何使用单个查询获取存储在不同变量中的多个表行的计数?

php - 如何在模式中插入 100000 条记录

php - Android总是抛出超时异常

mysql - 如果在 concat 里面

sql - 如何实现条件Upsert存储过程?

mysql - 在 MySQL 中找不到存储过程中的临时表

mysql - 获取存储更新的行和以前的行