SQL。如何从表中获得唯一值,但仅适用于1列?
我有一个桌子。我按名称对此表进行排序,按时间列:
SELECT name, value
FROM table1
WHERE time >= '2022-05-23 00:00:00' AND time <= '2022-05-23 01:00:00'
ORDER BY name, time
结果我得到了下一个表:
name, value, time
A, 5, 2022-05-23 01:01:12
A, 9, 2022-05-23 01:02:11
A, 7, 2022-05-23 01:03:21
B, 5, 2022-05-23 01:04:23
B, 6, 2022-05-23 01:05:33
C, 7, 2022-05-23 01:06:30
C, 8, 2022-05-23 01:07:41
C, 3, 2022-05-23 01:08:44
C, 7, 2022-05-23 01:09:50
然后我需要选择所有具有最低时间的唯一名称,并获得此名称的值:
结果应该像这样:
name, value, time
A, 5, 2022-05-23 01:01:12
B, 5, 2022-05-23 01:04:23
C, 7, 2022-05-23 01:06:30
================== =============================================== ============= = ==
我们刚从表中获取了每个第一个唯一名称:
name, value, time
A, 5, 2022-05-23 01:01:12 <- take this one
A, 9, 2022-05-23 01:02:11
A, 7, 2022-05-23 01:03:21
B, 5, 2022-05-23 01:04:23 <- take this one
B, 6, 2022-05-23 01:05:33
C, 7, 2022-05-23 01:06:30 <- take this one
C, 8, 2022-05-23 01:07:41
C, 3, 2022-05-23 01:08:44
C, 7, 2022-05-23 01:09:50
我的目标是以最低的时间戳(或表格中的每个第一个唯一名称,因为它已经由时间戳)
我不清楚如何获得想要的结果。 我尝试使用“选择不同的名称,值”,但它是所有唯一名称和唯一值,但是我只需要有唯一的名称+值(时间最低)
I have a table1 . I sort this table by name, and by time column :
SELECT name, value
FROM table1
WHERE time >= '2022-05-23 00:00:00' AND time <= '2022-05-23 01:00:00'
ORDER BY name, time
AS result i got next table :
name, value, time
A, 5, 2022-05-23 01:01:12
A, 9, 2022-05-23 01:02:11
A, 7, 2022-05-23 01:03:21
B, 5, 2022-05-23 01:04:23
B, 6, 2022-05-23 01:05:33
C, 7, 2022-05-23 01:06:30
C, 8, 2022-05-23 01:07:41
C, 3, 2022-05-23 01:08:44
C, 7, 2022-05-23 01:09:50
Then i need select all unique names with lowest time, and got values for this names:
Result should be like this:
name, value, time
A, 5, 2022-05-23 01:01:12
B, 5, 2022-05-23 01:04:23
C, 7, 2022-05-23 01:06:30
=====================================================================
It turns out that we just took each first unique name from the table:
name, value, time
A, 5, 2022-05-23 01:01:12 <- take this one
A, 9, 2022-05-23 01:02:11
A, 7, 2022-05-23 01:03:21
B, 5, 2022-05-23 01:04:23 <- take this one
B, 6, 2022-05-23 01:05:33
C, 7, 2022-05-23 01:06:30 <- take this one
C, 8, 2022-05-23 01:07:41
C, 3, 2022-05-23 01:08:44
C, 7, 2022-05-23 01:09:50
My goal is take unique names with lowest timestamp (or each first unique name from table, because it's already sorted by timestamp)
I'm not clear how to get wanted result.
I tried use "SELECT DISTINCT name,values" but it's back all unique names AND unique values, but i'm need got ONLY unique names+values (where time is lowest)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用函数
argmin
聚合函数( https://clickhouse.com/docs/en/sql-reference/aggregate-functions/reference/Reference/Argmin/ )以实现所需的结果。如果需要获取最新的行,则有gragmax
聚合函数( https://clickhouse.com/docs/en/sql-reference/aggregate-functions/reference/reference/argmax/ )。argmin/argmax
的想法是返回给定值的最小值或最大参数(在您的用例中,time
)。示例数据:
由于您需要整个行(和
argmin/argmax
需要一个参数),因此需要将其转换为另一个结构(此示例中的元组),然后将其投射回他们的名称。以下查询返回第一个(或val
表达式的最小值的参数):AS
gragmax
以类似的方式工作,但要获取最新的(或参数的参数)val
表达式的最大值),这将为您提供最新的(最新)名称/值/时间行:You can use the function
argMin
aggregation function (https://clickhouse.com/docs/en/sql-reference/aggregate-functions/reference/argmin/) to achieve the desired result. If you need to get the latest row, there is theargMax
aggregation function (https://clickhouse.com/docs/en/sql-reference/aggregate-functions/reference/argmax/).The idea of
argMin/argMax
is to return the argument for the min or max of a given value (time
, in your use case).Sample data:
As you need the whole row (and
argMin/argMax
require a single argument), it needs to be converted to another structure (a tuple in this example) and then projected back to their names. The following query returns the first (or the argument for the minimum value ofval
expression):As
argMax
works in a similar way but to get the latest (or argument for the maximum value of theval
expression), this will give you the latest (most recent) name/value/time rows:通常,您会使用
row_number
,但是ClickHouse不支持分析功能。我们可以使用加入方法:Generally you would use
ROW_NUMBER
, but Clickhouse does not support analytic functions. We can use a join approach instead: