> select * from yhh group by phone name: yhh tags: phone= time age blog id name ---- --- ---- -- ---- 1563889538654374538 26 http://blog.hhui.top 10 一灰灰 1563889547738266214 30 http://blog.hhui.top 11 一灰灰 1563889704754695002 30 http://blog.hhui.top 11 一灰灰2
name: yhh tags: phone=110 time age blog id name ---- --- ---- -- ---- 1563889723440000821 30 http://blog.hhui.top 11 一灰灰3
> select * from yhh group by age name: yhh tags: age= time age blog id name phone ---- --- ---- -- ---- ----- 1563889538654374538 26 http://blog.hhui.top 10 一灰灰 1563889547738266214 30 http://blog.hhui.top 11 一灰灰 1563889704754695002 30 http://blog.hhui.top 11 一灰灰2 1563889723440000821 30 http://blog.hhui.top 11 一灰灰3 110
从上面的case中可以看出,虽然执行了,但是返回的结果并不是我们预期的。
b. group by *
另外一个与一般SQL语法不一样的是group by 后面可以跟上*,表示根据所有的tag进行分组,一个测试如下
> select * from yhh group by * name: yhh tags: name=一灰灰, phone= time age blog id ---- --- ---- -- 1563889538654374538 26 http://blog.hhui.top 10 1563889547738266214 30 http://blog.hhui.top 11
name: yhh tags: name=一灰灰2, phone= time age blog id ---- --- ---- -- 1563889704754695002 30 http://blog.hhui.top 11
name: yhh tags: name=一灰灰3, phone=110 time age blog id ---- --- ---- -- 1563889723440000821 30 http://blog.hhui.top 11 >
> select * from yhh where time>'2019-07-23T13:44:38.654374538Z' and time<'2019-07-23T13:50:43.440000821Z' GROUP BY time(2m) ERR: GROUP BY requires at least one aggregate function
where条件限定查询的时间范围,否则会得到很多数据
group by time(2m) 表示每2分钟做一个分组, group by time(2s)则表示每2s做一个分组
# 根据非time进行排序时,直接报错 > select * from yhh order by age ERR: error parsing query: only ORDER BY time supported at this time # 根据时间进行倒排 > select * from yhh order by time desc name: yhh time age blog id name phone ---- --- ---- -- ---- ----- 2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 一灰灰3 110 2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11 一灰灰2 2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰 2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰 >
> select * from yhh limit 2 name: yhh time age blog id name phone ---- --- ---- -- ---- ----- 2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 一灰灰 2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11 一灰灰
# 分组之后,再限定查询条数 > select * from yhh group by "name"limit 1 name: yhh tags: name=一灰灰 time age blog id phone ---- --- ---- -- ----- 2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10
name: yhh tags: name=一灰灰2 time age blog id phone ---- --- ---- -- ----- 2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11
name: yhh tags: name=一灰灰3 time age blog id phone ---- --- ---- -- ----- 2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11 110
b. slimit
N指定从指定measurement返回的series数
1
SELECT_clause [INTO_clause] FROM_clause [WHERE_clause] GROUP BY *[,time(<time_interval>)] [ORDER_BY_clause] SLIMIT <N>
> select * from yhh group by * slimit 3 name: yhh tags: name=一灰灰, phone= time age blog id ---- --- ---- -- 2019-07-23T13:45:38.654374538Z 26 http://blog.hhui.top 10 2019-07-23T13:45:47.738266214Z 30 http://blog.hhui.top 11
name: yhh tags: name=一灰灰, phone=110 time age blog id ---- --- ---- -- 2019-08-14T11:18:06.804162557Z 14 http://spring.hhui.top 14 2019-08-14T11:18:10.146588721Z 15 http://spring.hhui.top 15 2019-08-14T11:18:12.753413004Z 16 http://spring.hhui.top 16
name: yhh tags: name=一灰灰2, phone= time age blog id ---- --- ---- -- 2019-07-23T13:48:24.754695002Z 30 http://blog.hhui.top 11
name: yhh tags: name=一灰灰3, phone=110 time age blog id ---- --- ---- -- 2019-07-23T13:48:43.440000821Z 30 http://blog.hhui.top 11