- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试获取旅行的实时温度,如果实时数据不可用,则从历史数据中获取平均温度。我用这些表格制作了我的问题的简单版本:
旅行
id departure_time arrival_time location_id
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1
位置
id name
1 Location
天气
id temperature date location_id
1 20 2018-04-07 1
2 20 2018-04-08 1
3 20 2018-04-09 1
4 20 2018-04-10 1
5 20 2018-04-11 1
6 20 2018-04-12 1
7 20 2018-04-13 1
8 20 2018-04-14 1
9 15 2016-04-07 1
10 15 2016-04-08 1
11 15 2016-04-09 1
12 15 2016-04-10 1
13 15 2016-04-11 1
14 15 2016-04-12 1
15 15 2016-04-13 1
16 15 2016-04-14 1
17 19 2017-04-07 1
18 19 2017-04-08 1
19 19 2017-04-09 1
20 19 2017-04-10 1
21 19 2017-04-11 1
22 19 2017-04-12 1
23 19 2017-04-13 1
24 19 2017-04-14 1
25 15 2017-04-15 1
26 15 2017-04-16 1
27 15 2017-04-17 1
28 15 2017-04-18 1
29 15 2017-04-19 1
30 15 2017-04-20 1
31 15 2017-04-21 1
32 19 2016-04-15 1
33 19 2016-04-16 1
34 19 2016-04-17 1
35 19 2016-04-18 1
36 19 2016-04-19 1
37 19 2016-04-20 1
38 19 2016-04-21 1
我遇到的问题是,由于这些旅行是最后一分钟的旅行,我有下周内出发的旅行的“实时”数据。所以我想获得实时预报(如果有的话),或者获得前几年温度的平均值。
http://sqlfiddle.com/#!17/bce59/3
这是我为尝试解决问题而采用的方法。
如果忘记了任何细节,请询问。
预期结果:
id departure_time arrival_time location_id temperature
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
1 2018-04-07 07:00:00 2018-04-14 17:00:00 1 20
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 20
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 17
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 17
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 17
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 17
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 17
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 17
2 2018-04-14 07:00:00 2018-04-21 17:00:00 1 17
最佳答案
使用 generate_series
函数从子查询上的 trip
表创建日历。
然后 Left JOIN
在 dates
的子查询上你可能会得到匹配的 weather
你可以得到它的温度。如果 temperature
在 w.temperature
上为空,则获取 avg
temperature
你可以试试这个。
SELECT t.id,
t.departure_time,
t.arrival_time,
l.id as "location_id",
coalesce(w.temperature,(select FLOOR(avg(temperature)) from weather)) as "temperature"
FROM
location l inner join
(
select id,
location_id,
departure_time,
arrival_time,
generate_series(departure_time :: timestamp,arrival_time::timestamp,'1 day'::interval) as dates
from trip
) t on t.location_id = l.id LEFT JOIN weather w on t.dates::date = w.date::date
sqlfiddle:http://sqlfiddle.com/#!17/bce59/48
编辑
您可以使用 CTE 查询按 year
获取 Avg
而不是 coalesce
函数中的子查询条款。
WITH weather_avg AS (
SELECT floor(avg(a)) avgTemp
from
(
SELECT
extract(YEAR from weather.date) AS YEAR,
floor(avg(weather.temperature)) a
FROM weather
group by extract(YEAR from weather.date)
) t
)
SELECT t.id,
t.departure_time,
t.arrival_time,
t.location_id as "location_id",
coalesce(w.temperature,(select avgTemp from weather_avg)) as "temperature"
FROM
(
select t.id,
t.location_id,
t.departure_time,
t.arrival_time,
generate_series(departure_time :: timestamp,arrival_time::timestamp,'1 day'::interval) as dates
from trip t inner join location l on t.location_id = l.id
) t LEFT JOIN weather w
on t.dates::date = w.date::date
sqlfiddle:http://sqlfiddle.com/#!17/bce59/76
关于sql - 如果可用,从实时数据中获取温度,否则对历史数据取平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49977629/
谁能告诉我下面的代码是如何工作的, #define func(t, a, b){ t temp; temp=a; a=b; b=temp;} int main() { int a=3, b=4; f
private static final Group[] toGroups (String string) { int partialGroupSize = strin
我想更改 svg 的此文本部分: **--** 我想显示来自 KNX 安装的温度我已尝试使用以下代码,但没有任何反应: 以 $('#VL_temp' 开头的行有什么问题?其余代码似乎没问题,
目标 测量我的 Linux Box 的 CPU 温度。 到目前为止工作已完成 我已经安装了 lm-sensors 来检测温度,下面是命令传感器的输出: root@XXXX-XX :# sensors
我知道以某种方式获取 CPU 的温度是可能的,因为我下载了一个应用程序,它可以在没有开启Root的设备上执行此操作。它是怎么做到的? 编辑:该应用程序称为 A1 CPU 工具。我花了几天时间寻找答案。
我对如何使用一些视频卡驱动程序 API 读取 GPU 温度(图形处理单元,显卡主芯片)的方法感兴趣? 每个人都知道有两个不同的芯片制造商(至少是流行的)- ATI 和 nVIDIA - 因此有两种不同
我有一个运行 Windows 10 IoT 核心版的 Raspberry Pi 3。我的问题是;是否有可能,或者是否有一种方法可以通过远程 PowerShell 连接、在设备本地的命令行控制台中监控设
这里我真的很疑惑。我想创建一个应用程序,它在我的显卡(AMD 显卡)的不同温度下执行不同的事件。 我想制作这样一个应用程序的原因是,对于 GPU,我还没有找到一个,其次是为了确保我永远不会因为达到巨大
我想获取 CPU 温度。下面是我使用 C++ 和 WMI 所做的。我正在阅读 MSAcpi_ThermalZoneTemperature,但它总是一样的,根本不是 CPU 温度。 有没有什么办法不用写
我正在尝试使用 Raspberry Pi 上的 DS18B20 传感器读取温度。我的问题是从这个传感器读取数据需要时间。它不多,或多或少 1 秒,但我不能让我的主程序等到这完成。我不需要有“最近的值(
我正在为系统性能创建一个个人监控程序,但我在弄清楚 C# 如何检索 CPU 和 GPU 温度信息时遇到了问题。 我已经让程序通过 PerformanceCounter 检索 CPU 负载和频率信息(以
我需要使用 Swift 获取 CPU 温度,但除了 this 之外我找不到任何信息. 我认为我应该使用 IOKit.framework 但同样没有太多关于它的信息。 最佳答案 使用 https://g
我只是想知道。在 PHP 中设置一个没有过期的 cookie(意味着它在浏览器关闭时过期)和设置一个 session 变量之间有什么区别。我不是在谈论登录之类的东西;而不是需要在每次访问页面时获取不太
本文实例讲述了android电源信息查看方法。分享给大家供大家参考。具体如下: 1. PowerTestActivity: ?
我有一个通过 http 连接的温度传感器,我想每 5 秒向 Amcharts 添加一次温度。有人有一些如何向当前数据集添加新值的示例吗?我将使用折线图来表示 y 轴上的温度和 x 轴上的日期时间 谢谢
大家好,我正在尝试通过 php.wsdl Web 服务发送我的温度和湿度传感器,我需要将数据解析到 Web 服务,以便我可以将其从 Web 服务插入 mysql 数据库.. 我遇到了一些问题,请指教
如何从 Linux 上的 bash 获取平均 CPU 温度?最好是华氏度。该脚本应该能够处理不同数量的 CPU。 最佳答案 你这样做: 安装 sudo apt install lm-sensors s
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Getting CPU temperature using Python? 最简单的方法是什么?也最好是摄氏
如何在 Linux 上获取 CPU 的当前温度? 有several questions and answers关于使用 C# 获取 CPU 温度,但它们似乎都是特定于 Windows 的。 最佳答案
我正在使用此代码获取当前 CPU 温度: 并看到了it也是 private float getCurrentCPUTemperature() { String file = readFile
我是一名优秀的程序员,十分优秀!