@Test
  public void groupByTimeOrderByTimeTest3() {
    String sql =
        "SELECT AVG(*),COUNT(*),MAX_VALUE(*) FROM root.weather.** GROUP BY([2022-11-21T00:00:00.000+08:00,2022-11-21T00:00:02.801+08:00),1000ms) ORDER BY TIME ASC,DEVICE DESC ALIGN BY DEVICE";
    try (Connection connection = EnvFactory.getEnv().getConnection();
        Statement statement = connection.createStatement()) {
      long lastTime = -1;
      String lastDevice = "";
      int index = 0;
      try (ResultSet resultSet = statement.executeQuery(sql)) {
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        checkHeader(
            resultSetMetaData,
            "Time,Device,AVG(precipitation),AVG(temperature),COUNT(precipitation),COUNT(temperature),MAX_VALUE(precipitation),MAX_VALUE(temperature)");
        int cnt = 0;
        while (resultSet.next()) {
          long actualTime = resultSet.getLong(1);
          String actualDevice = resultSet.getString(2);
          assertTrue(actualTime >= lastTime);
          if (!Objects.equals(lastDevice, "") && actualTime == lastTime)
            assertTrue(actualDevice.compareTo(lastDevice) <= 0);
          lastTime = actualTime;
          lastDevice = actualDevice;
          double avgPrecipitation = resultSet.getDouble(3);
          assertTrue(deviceToAvgPrecipitation.get(actualDevice)[cnt] - avgPrecipitation < 0.00001);

          double avgTemperature = resultSet.getDouble(4);
          assertTrue(deviceToAvgTemperature.get(actualDevice)[cnt] - avgTemperature < 0.00001);

          int countPrecipitation = resultSet.getInt(5);
          assertEquals(getCountNum(actualDevice, cnt), countPrecipitation);
          int countTemperature = resultSet.getInt(6);
          assertEquals(getCountNum(actualDevice, cnt), countTemperature);

          long maxPrecipitation = resultSet.getLong(7);
          assertEquals(deviceToMaxPrecipitation.get(actualDevice)[cnt], maxPrecipitation);

          double maxTemperature = resultSet.getDouble(8);
          assertTrue(deviceToMaxTemperature.get(actualDevice)[cnt] - maxTemperature < 0.00001);
          index++;
          if (index % 10 == 0) cnt++;
        }
        assertEquals(30, index);
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
This method generate test data with crossing time.

<p>The data can be viewed in online doc:

<p>https://docs.google.com/spreadsheets/d/18XlOIi27ZIIdRnar2WNXVMxkZwjgwlPZmzJLVpZRpAA/edit#gid=0
integration-test/src/test/java/org/apache/iotdb/db/it/alignbydevice/IoTDBOrderByWithAlignByDeviceIT.java