求助:请大师帮忙看看如何优雅的求出每天不同时段的平均温度(有图)

时间与温度
这个是每天时间与温度对应的一个数组

下面是用户自定义的时间段
自定义时间段

需求是,求出每个不同时间段,平均温度值!

我都写好几天了,都不行,那位大师帮帮忙吧,谢谢!

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 5

查出当天用户最小开始时间和最晚结束时间内的数据,然后循环匹配求平均数呗

3年前 评论
dongzhiyu (楼主) 3年前
spsq009 (作者) 3年前

数据库搞多个时间段的表,关联上时间点的温度表,不管是查,还是分组计算,都好搞。要不然你就老实循环计算

3年前 评论
dongzhiyu (楼主) 3年前

每天的时间段是固定的还是用户可以随意选择时间段?

3年前 评论
24K大白羊 (作者) 3年前
dongzhiyu (楼主) 3年前
24K大白羊 (作者) 3年前
dongzhiyu (楼主) 3年前
24K大白羊 (作者) 3年前
bing8u

快夸我 :smirk:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;

class TestController extends Controller
{
    public function __invoke(Request $request)
    {
        $dates = [
            ['2020-11-09 04:00:00', '2020-11-09 08:00:00'],
            ['2020-11-09 08:00:00', '2020-11-09 11:00:00'],
            ['2020-11-09 11:00:00', '2020-11-09 15:00:00'],
            ['2020-11-09 15:00:00', '2020-11-09 22:00:00'],
            ['2020-11-09 22:00:00', '2020-11-10 04:00:00'],
            ['2020-11-10 04:00:00', '2020-11-10 08:00:00'],
            ['2020-11-10 08:00:00', '2020-11-12 11:00:00'],
            ['2020-11-10 11:00:00', '2020-11-12 15:00:00'],
        ];

        return Collection::make($dates)->map(function ($val) {
            $avg = $this->getAvgItem($val[0], $val[1])->avg();

            return [
                'start' => $val[0],
                'end' => $val[1],
                'avg' => $avg
            ];
        });
    }


    protected function getAvgItem(string $start, string $end)
    {
        $start_date = Carbon::parse($start);
        $end_date = Carbon::parse($end);

        $data = $this->getTemperature();

        return Collection::make($data)->filter(function ($key, $val) use ($start_date, $end_date) {
            $now_date = Carbon::parse($val);

            return $now_date->gte($start_date) && $now_date->lte($end_date);
        });
    }

    protected function getTemperature()
    {
        return [
            '2020-11-09 00:00:00' => 0,
            '2020-11-09 01:00:00' => 0,
            '2020-11-09 02:00:00' => -1,
            '2020-11-09 03:00:00' => -1,
            '2020-11-09 04:00:00' => -2,
            '2020-11-09 05:00:00' => -2,
            '2020-11-09 06:00:00' => -2,
            '2020-11-09 07:00:00' => -3,
            '2020-11-09 08:00:00' => -3,
            '2020-11-09 09:00:00' => 0,
            '2020-11-09 10:00:00' => 3,
            '2020-11-09 11:00:00' => 6,
            '2020-11-09 12:00:00' => 8,
            '2020-11-09 13:00:00' => 11,
            '2020-11-09 14:00:00' => 13,
            '2020-11-09 15:00:00' => 13,
            '2020-11-09 16:00:00' => 12,
            '2020-11-09 17:00:00' => 12,
            '2020-11-09 18:00:00' => 9,
            '2020-11-09 19:00:00' => 7,
            '2020-11-09 20:00:00' => 6,
            '2020-11-09 21:00:00' => 6,
            '2020-11-09 22:00:00' => 6,
            '2020-11-09 23:00:00' => 4,

            '2020-11-10 00:00:00' => 2,
            '2020-11-10 01:00:00' => 2,
            '2020-11-10 02:00:00' => 1,
            '2020-11-10 03:00:00' => 1,
            '2020-11-10 04:00:00' => 0,
            '2020-11-10 05:00:00' => 0,
            '2020-11-10 06:00:00' => 0,
            '2020-11-10 07:00:00' => 0,
            '2020-11-10 08:00:00' => 0,
            '2020-11-10 09:00:00' => 8,
            '2020-11-10 10:00:00' => 7,
            '2020-11-10 11:00:00' => 10,
            '2020-11-10 12:00:00' => 11,
            '2020-11-10 13:00:00' => 12,
            '2020-11-10 14:00:00' => 13,
            '2020-11-10 15:00:00' => 13,
        ];
    }
}
3年前 评论
dongzhiyu (楼主) 3年前
dongzhiyu (楼主) 3年前
dongzhiyu (楼主) 3年前
我爱大可乐 3年前
keyboby 3年前
sodasix 3年前
xylp
 select DATE_FORMAT(日期,"%Y-%m-%d") as day, avg(温度) as av fromgroup by  DATE_FORMAT(日期,"%Y-%m-%d")
3年前 评论
dongzhiyu (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!