如何在涌入数据库中使用Distinct函数

Amm*_*mad 9 distinct distinct-values influxdb

我正在使用涌入数据库和发出命令,

SELECT * FROM interface
Run Code Online (Sandbox Code Playgroud)

以下是输出 -

interface 
time                              element                path                                       value
2016-08-24T21:22:16.7080877Z    "link-layer-address0"   "key:/arp-information/link-layer-address0"  "3c:61:04:48:df:91"
2016-08-24T21:22:17.9090527Z    "link-layer-address0"   "key:/arp-information/link-layer-address0"  "3c:61:04:48:df:92"
2016-08-24T21:22:19.8584133Z    "link-layer-address1"   "key:/arp-information/link-layer-address1"  "3c:61:04:48:df:97"
2016-08-24T21:22:20.3377847Z    "link-layer-address2"   "key:/arp-information/link-layer-address2"  "3c:61:04:48:df:90"
Run Code Online (Sandbox Code Playgroud)

当发出命令它工作正常.

SELECT distinct(value) FROM interface 
Run Code Online (Sandbox Code Playgroud)

但是当路径列的发出命令没有输出时.想知道我错过了什么?

SELECT distinct(path) FROM interface 
Run Code Online (Sandbox Code Playgroud)

Jas*_*son 14

感谢@Ammad的额外信息.

简答

尝试GROUP BY使用标签.DISTINCT()仅适用于字段.

答案很长

distinct()适用于字段,而不适用于标记.看这里:

https://docs.influxdata.com/influxdb/v1.0/query_language/functions/#distinct

DISTINCT()返回单个字段的唯一值.

字段值是您感兴趣的实际数据.标记值是元数据:有关数据的数据.数据库系统中的大多数功能都在数据或元数据上运行,但两者都很少.

这是v0.13上的一个玩具示例,显示它distinct()实际上不适用于标记:

insert foo,tag1=asdf field1="some text"
insert foo,tag1=asdf field1="some text"
insert foo,tag1=asdfg field1="some text"
insert foo,tag1=asdfg field1="some text"
insert foo,tag1=asdfg field1="some more text"
insert foo,tag1=asdfg field1="some more text"
Run Code Online (Sandbox Code Playgroud)

现在一些问题:

select * from foo

name: foo
time                            field1          tag1
2016-09-12T05:19:53.563221799Z  some text       asdf
2016-09-12T05:20:03.027652248Z  some text       asdf
2016-09-12T05:20:10.04939971Z   some text       asdfg
2016-09-12T05:20:11.235525548Z  some text       asdfg
2016-09-12T05:20:17.418920163Z  some more text  asdfg
2016-09-12T05:20:19.354742922Z  some more text  asdfg
Run Code Online (Sandbox Code Playgroud)

现在让我们试试吧 distinct()

select distinct(tag1) from foo
Run Code Online (Sandbox Code Playgroud)

结果根本没有输出.

select distinct(field1) from foo

name: foo
time                    distinct
1970-01-01T00:00:00Z    some text
1970-01-01T00:00:00Z    some more text
Run Code Online (Sandbox Code Playgroud)

您可以通过使用获得所需的内容GROUP BY.像这样:

select distinct(field1) from foo group by tag1
Run Code Online (Sandbox Code Playgroud)

这使:

name: foo
tags: tag1=asdf
time                    distinct
1970-01-01T00:00:00Z    some text

name: foo
tags: tag1=asdfg
time                    distinct
1970-01-01T00:00:00Z    some text
1970-01-01T00:00:00Z    some more text
Run Code Online (Sandbox Code Playgroud)

这显示了该值的每个值tag1,以及与该值field1关联的tag1值.

希望有所帮助.


小智 11

有一种方法可以通过使用双SELECT语句来解决这个问题:

> select distinct("tag1") from (select "field1", "tag1" from foo)
Run Code Online (Sandbox Code Playgroud)

内部查询返回field1和tag1,可以在外部查询,就像您可以应用distinct()的普通字段一样.

希望能帮助到你.科斯莫.

  • 这会在InfluxDB v1.6上为我返回零行 (2认同)
  • 是的,内部查询返回行正常. (2认同)
  • 这对我有用,但如果我的表中有无数行,但只有 10 个不同的标签,那么它将对无数行进行排序以运行不同的函数。如何限制“内部”查询检索的数据,如下所示:`select unique("tag1") from (select "field1", "tag1" from foo where time>now()-10m)`请注意“内部”这可能是一个不好的术语,因为“内部”查询通常引用其他域中的关系表连接类型 (2认同)

小智 8

SHOW TAG VALUES from "measurements" WITH key = path
Run Code Online (Sandbox Code Playgroud)

例如:

SHOW TAG VALUES from redis with key="server"
Run Code Online (Sandbox Code Playgroud)


Ale*_*der 5

SHOW TAG VALUES WITH key = path一个可以用来获取唯一的标签值

  • 要限制测量,可以使用key = path从“测量名称”中显示标签值 (2认同)