gpt4 book ai didi

mysql - 获取日期时间从/到特定时间之间的平均计数

转载 作者:行者123 更新时间:2023-11-29 06:08:46 25 4
gpt4 key购买 nike

我想对我的数据库中的记录进行统计,我想计算用户从/到某个日期时间以及每天每 4 小时登录系统的平均次数

简单示例:我想获得从“2016-09-20 00:00:00”到“2016-09-23 23:59:59”的成功登录平均值次 ('00:00:00' - '11:59:59') 和 ('12:00:00' - '23:59:59')

这是一个示例数据列表(其中状态 1 表示成功,0 表示不成功):

| id | | driver_id |    login_timedate     | status |
| 1 | | 1 | '2016-09-20 00:00:11' | 1 |
| 2 | | 2 | '2016-09-20 01:16:09' | 1 |
| 3 | | 2 | '2016-09-20 23:01:16' | 1 |
| 4 | | 3 | '2016-09-21 04:04:59' | 1 |
| 5 | | 3 | '2016-09-21 05:06:59' | 0 |
| 6 | | 2 | '2016-09-21 16:06:59' | 1 |
| 7 | | 1 | '2016-09-22 00:16:59' | 1 |
| 8 | | 2 | '2016-09-23 04:09:22' | 0 |
| 9 | | 1 | '2016-09-23 06:22:59' | 1 |
| 10 | | 3 | '2016-09-23 22:09:22' | 1 |
| 11 | | 1 | '2016-09-24 00:00:22' | 1 |
  • 所以在这种情况下,我将从 (20-23/09/2016) 获得的成功登录总数为:8(第 1 天= 3 , day2= 2 , day3= 1 , day4= 2)

  • 每天在 ('00:00:00' - '11:59:59') 是 5 (day1= 2 , day2= 1 , day3= 1 , 第 4 天= 1)

  • 平均值: 5/4 = 1.25

  • 每天在 ('00:00:00' - '11:59:59') 是 3 (day1= 1 , day2= 0 , day3= 1 , day4= 1)

  • 平均值:3/4 = 0.75

我已经完成了第一部分以获取日期时间范围内的成功登录总数这是我的代码(将返回 8)

SET @start_date = '2016-09-20';
SET @start_taime = '00:00:00';

SET @end_date = '2016-09-23';
SET @end_time = '23:59:59';

SELECT SUM(`total_logins`.`number_of_success`) FROM (
SELECT COUNT( `login_logs`.`driver_id` ) AS `number_of_success`
FROM `login_logs`
WHERE `login_logs`.`status` = 1
AND
`login_logs`.`login_timedate` >= CONCAT(@start_date, ' ', @start_time)
AND
`login_logs`.`login_timedate` <= CONCAT(@end_date, ' ', @end_time)
GROUP BY `login_logs`.`user_id`
) AS `total_logins`

#更新:此代码的预期输出:

| total_logins | 
| 8 |

我想做下一部分,计算从 XX:XX:XX 时间到 YY:YY:YY 时间的相同日期时间范围内的平均登录数,如下所示:

  • Total number of success each day within the range from ('00:00:00' - '11:59:59') are 5 (day1= 2 , day2= 1 , day3= 1 , day4= 1)
    • Average: 5 / 4 = 1.25

#更新:修改我的代码以从 ('00:00:00' - '11:59:59') 获取平均值后的预期输出:

|  Avrage_00_12   | 
| 1.25 |

我应该如何修改代码来实现这部分?

希望你能理解我的问题

感谢您在高级方面的帮助

最佳答案

您可以使用以下查询:

SELECT SUM(`number_of_success`) AS `total_success`,
SUM(`success_range1`) / COUNT(*) AS `average1`,
SUM(`success_range2`) / COUNT(*) AS `average2`
FROM (
SELECT DATE(`login_logs`.`login_timedate`),
COUNT( `login_logs`.`driver_id` ) AS `number_of_success`,
COUNT(CASE
WHEN TIME(`login_logs`.`login_timedate`)
BETWEEN '00:00:00' AND '11:59:59'
THEN 1
END) AS `success_range1`,
COUNT(CASE WHEN TIME(`login_logs`.`login_timedate`)
BETWEEN '12:00:00' AND '23:59:59'
THEN 1
END) AS `success_range2`
FROM `login_logs`
WHERE `login_logs`.`status` = 1
AND
`login_logs`.`login_timedate` >= '2016-09-20 00:00:00'
AND
`login_logs`.`login_timedate` <= '2016-09-23 23:59:59'
GROUP BY DATE(`login_logs`.`login_timedate`)) AS t

输出:

total_success, average1, average2
----------------------------------
8, 1.2500, 0.7500

关于mysql - 获取日期时间从/到特定时间之间的平均计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39663808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com