0

为什么使用 TimescaleDB 而不是关系数据库?文章以下示例是针对双时间用例给出的

SELECT
    time_bucket('1 day', time) AS day,
    asset_code,
    last(price, time_recorded)
FROM prices
WHERE time > '2017-01-01'
GROUP BY day, asset_code
ORDER BY day DESC, asset_code;

如果我们假设prices表包含recorded_by 列,是否可以编写一个同时返回time_recorded 和recorded_by 列的查询?

例如,给定以下输入:

+------------------+------------+-------+------------------+-------------+
|       time       | asset_code | price |  time_recorded   | recorded_by |
+------------------+------------+-------+------------------+-------------+
| 2019-08-08 12:00 |          1 | 9     | 2019-08-08 12:00 | foo         |
| 2019-08-09 15:30 |          1 | 10    | 2019-08-09 15:30 | foo         |
| 2019-08-08 12:00 |          1 | 9.5   | 2019-08-09 15:00 | bar         |
+------------------+------------+-------+------------------+-------------+

您会期望以下输出:

+------------+------------+-------+------------------+-------------+
|    day     | asset_code | price |  time_recorded   | recorded_by |
+------------+------------+-------+------------------+-------------+
| 2019-08-09 |          1 | 10    | 2019-08-09 15:30 | foo         |
| 2019-08-08 |          1 | 9.5   | 2019-08-09 13:00 | bar         |
+------------+------------+-------+------------------+-------------+
4

1 回答 1

1

事实证明,您实际上可以让最后一个函数的两个参数都引用同一列,例如

SELECT
    time_bucket('1 day', time) AS day,
    asset_code,
    last(price, time_recorded) AS price,
    last(time_recorded, time_recorded) AS time_recorded,
    last(recorded_by, time_recorded) AS recorded_by
FROM prices
WHERE time > '2017-01-01'
GROUP BY day, asset_code
ORDER BY day DESC, asset_code;
于 2019-08-12T14:48:25.700 回答