跳至主要內容

...大约 4 分钟

结果集排序

时间对齐模式下的排序

IoTDB的查询结果集默认按照时间对齐,可以使用ORDER BY TIME的子句指定时间戳的排列顺序。示例代码如下:

select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc;

执行结果:

+-----------------------------+--------------------------+------------------------+-----------------------------+------------------------+
|                         Time|root.ln.wf02.wt02.hardware|root.ln.wf02.wt02.status|root.ln.wf01.wt01.temperature|root.ln.wf01.wt01.status|
+-----------------------------+--------------------------+------------------------+-----------------------------+------------------------+
|2017-11-01T00:01:00.000+08:00|                        v2|                    true|                        24.36|                    true|
|2017-11-01T00:00:00.000+08:00|                        v2|                    true|                        25.96|                    true|
|1970-01-01T08:00:00.002+08:00|                        v2|                   false|                         null|                    null|
|1970-01-01T08:00:00.001+08:00|                        v1|                    true|                         null|                    null|
+-----------------------------+--------------------------+------------------------+-----------------------------+------------------------+
Total line number = 4

设备对齐模式下的排序

当使用ALIGN BY DEVICE查询对齐模式下的结果集时,可以使用ORDER BY子句对返回的结果集顺序进行规定。

在设备对齐模式下支持4种排序模式的子句,其中包括两种排序键,DEVICETIME,靠前的排序键为主排序键,每种排序键都支持ASCDESC两种排列顺序。

  1. ORDER BY DEVICE: 按照设备名的字典序进行排序,排序方式为字典序排序,在这种情况下,相同名的设备会以组的形式进行展示。

  2. ORDER BY TIME: 按照时间戳进行排序,此时不同的设备对应的数据点会按照时间戳的优先级被打乱排序。

  3. ORDER BY DEVICE,TIME: 按照设备名的字典序进行排序,设备名相同的数据点会通过时间戳进行排序。

  4. ORDER BY TIME,DEVICE: 按照时间戳进行排序,时间戳相同的数据点会通过设备名的字典序进行排序。

为了保证结果的可观性,当不使用ORDER BY子句,仅使用ALIGN BY DEVICE时,会为设备视图提供默认的排序方式。其中默认的排序视图为ORDER BY DEVCE,TIME,默认的排序顺序为ASC
即结果集默认先按照设备名升序排列,在相同设备名内再按照时间戳升序排序。

当主排序键为DEVICE时,结果集的格式与默认情况类似:先按照设备名对结果进行排列,在相同的设备名下内按照时间戳进行排序。示例代码如下:

select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device;

执行结果:

+-----------------------------+-----------------+--------+------+-----------+
|                         Time|           Device|hardware|status|temperature|
+-----------------------------+-----------------+--------+------+-----------+
|1970-01-01T08:00:00.001+08:00|root.ln.wf02.wt02|      v1|  true|       null|
|1970-01-01T08:00:00.002+08:00|root.ln.wf02.wt02|      v2| false|       null|
|2017-11-01T00:00:00.000+08:00|root.ln.wf02.wt02|      v2|  true|       null|
|2017-11-01T00:01:00.000+08:00|root.ln.wf02.wt02|      v2|  true|       null|
|2017-11-01T00:00:00.000+08:00|root.ln.wf01.wt01|    null|  true|      25.96|
|2017-11-01T00:01:00.000+08:00|root.ln.wf01.wt01|    null|  true|      24.36|
+-----------------------------+-----------------+--------+------+-----------+
Total line number = 6

主排序键为Time时,结果集会先按照时间戳进行排序,在时间戳相等时按照设备名排序。
示例代码如下:

select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device;

执行结果:

+-----------------------------+-----------------+--------+------+-----------+
|                         Time|           Device|hardware|status|temperature|
+-----------------------------+-----------------+--------+------+-----------+
|1970-01-01T08:00:00.001+08:00|root.ln.wf02.wt02|      v1|  true|       null|
|1970-01-01T08:00:00.002+08:00|root.ln.wf02.wt02|      v2| false|       null|
|2017-11-01T00:00:00.000+08:00|root.ln.wf02.wt02|      v2|  true|       null|
|2017-11-01T00:00:00.000+08:00|root.ln.wf01.wt01|    null|  true|      25.96|
|2017-11-01T00:01:00.000+08:00|root.ln.wf02.wt02|      v2|  true|       null|
|2017-11-01T00:01:00.000+08:00|root.ln.wf01.wt01|    null|  true|      24.36|
+-----------------------------+-----------------+--------+------+-----------+
Total line number = 6

当没有显式指定时,主排序键默认为Device,排序顺序默认为ASC,示例代码如下:

select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device;

结果如图所示,可以看出,ORDER BY DEVICE ASC,TIME ASC就是默认情况下的排序方式,由于ASC是默认排序顺序,此处可以省略。

+-----------------------------+-----------------+--------+------+-----------+
|                         Time|           Device|hardware|status|temperature|
+-----------------------------+-----------------+--------+------+-----------+
|2017-11-01T00:00:00.000+08:00|root.ln.wf01.wt01|    null|  true|      25.96|
|2017-11-01T00:01:00.000+08:00|root.ln.wf01.wt01|    null|  true|      24.36|
|1970-01-01T08:00:00.001+08:00|root.ln.wf02.wt02|      v1|  true|       null|
|1970-01-01T08:00:00.002+08:00|root.ln.wf02.wt02|      v2| false|       null|
|2017-11-01T00:00:00.000+08:00|root.ln.wf02.wt02|      v2|  true|       null|
|2017-11-01T00:01:00.000+08:00|root.ln.wf02.wt02|      v2|  true|       null|
+-----------------------------+-----------------+--------+------+-----------+
Total line number = 6

同样,可以在聚合查询中使用ALIGN BY DEVICEORDER BY子句,对聚合后的结果进行排序,示例代码如下所示:

select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device

执行结果:

+-----------------------------+-----------------+---------------+-------------+------------------+
|                         Time|           Device|count(hardware)|count(status)|count(temperature)|
+-----------------------------+-----------------+---------------+-------------+------------------+
|2017-11-01T00:01:00.000+08:00|root.ln.wf01.wt01|           null|            1|                 1|
|2017-11-01T00:02:00.000+08:00|root.ln.wf01.wt01|           null|            0|                 0|
|2017-11-01T00:03:00.000+08:00|root.ln.wf01.wt01|           null|            0|                 0|
|2017-11-01T00:01:00.000+08:00|root.ln.wf02.wt02|              1|            1|              null|
|2017-11-01T00:02:00.000+08:00|root.ln.wf02.wt02|              0|            0|              null|
|2017-11-01T00:03:00.000+08:00|root.ln.wf02.wt02|              0|            0|              null|
+-----------------------------+-----------------+---------------+-------------+------------------+
Total line number = 6

Copyright © 2024 The Apache Software Foundation.
Apache IoTDB, IoTDB, Apache, the Apache feather logo, and the Apache IoTDB project logo are either registered trademarks or trademarks of The Apache Software Foundation in all countries

Have a question? Connect with us on QQ, WeChat, or Slack. Join the community now.