gpt4 book ai didi

Sql DISTINCT 从结果中删除列

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

我有一个简单的 PostgreSQL 查询,它按最近的日期查询事件,但是我想从结果中删除这个不同的列(时间)

SELECT DISTINCT time, sensor_id, event_type, value from events ORDER BY sensor_id

+---------------------+---+---+-----+
| 2014-02-13 12:42:00 | 2 | 2 | 5 |
| 2014-02-13 13:19:57 | 2 | 4 | -42 |
| 2014-02-13 13:32:36 | 2 | 3 | 54 |
| 2014-02-13 14:48:30 | 2 | 2 | 2 |
| 2014-02-13 12:54:39 | 3 | 2 | 7 |
+---------------------+---+---+-----+

需要这样的结果

+---+---+-----+
| 2 | 2 | 5 |
| 2 | 4 | -42 |
| 2 | 2 | 2 |
| 2 | 3 | 54 |
| 3 | 2 | 7 |
+---+---+-----+

最佳答案

您可以使用 PostgreSQL DISTINCT ON (...)特点:

SELECT DISTINCT ON (time, sensor_id, event_type, value)
sensor_id, event_type, value from events
ORDER BY sensor_id;

关于Sql DISTINCT 从结果中删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37811513/

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