07-function.md 59.2 KB
Newer Older
D
dingbo 已提交
1
---
2 3
sidebar_label: Functions
title: Functions
D
dingbo 已提交
4 5
---

6
## Aggregate Functions
D
dingbo 已提交
7

8
Aggregate query is supported in TDengine by following aggregate functions and selection functions.
D
dingbo 已提交
9 10 11 12 13 14 15

### COUNT

```
SELECT COUNT([*|field_name]) FROM tb_name [WHERE clause];
```

16
**Description**:Get the number of rows or the number of non-null values in a table or a super table.
D
dingbo 已提交
17

18
**Return value type**:Long integer INT64
D
dingbo 已提交
19

20
**Applicable column types**:All
D
dingbo 已提交
21

22
**Applicable table types**: table, super table, sub table
D
dingbo 已提交
23

24
**More explanation**:
D
dingbo 已提交
25

26 27
- Wildcard (\*) can be used to represent all columns, it's used to get the number of all rows
- The number of non-NULL values will be returned if this function is used on a specific column
D
dingbo 已提交
28

29
**Examples**:
D
dingbo 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

```
taos> SELECT COUNT(*), COUNT(voltage) FROM meters;
    count(*)        |    count(voltage)     |
================================================
                    9 |                     9 |
Query OK, 1 row(s) in set (0.004475s)

taos> SELECT COUNT(*), COUNT(voltage) FROM d1001;
    count(*)        |    count(voltage)     |
================================================
                    3 |                     3 |
Query OK, 1 row(s) in set (0.001075s)
```

### AVG

```
SELECT AVG(field_name) FROM tb_name [WHERE clause];
```

51
**Description**:Get the average value of a column in a table or stable
D
dingbo 已提交
52

53
**Return value type**:Double precision floating number
D
dingbo 已提交
54

55
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
56

57
**Applicable table types**:table, stable
D
dingbo 已提交
58

59
**Examples**:
D
dingbo 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

```
taos> SELECT AVG(current), AVG(voltage), AVG(phase) FROM meters;
    avg(current)        |       avg(voltage)        |        avg(phase)         |
====================================================================================
            11.466666751 |             220.444444444 |               0.293333333 |
Query OK, 1 row(s) in set (0.004135s)

taos> SELECT AVG(current), AVG(voltage), AVG(phase) FROM d1001;
    avg(current)        |       avg(voltage)        |        avg(phase)         |
====================================================================================
            11.733333588 |             219.333333333 |               0.316666673 |
Query OK, 1 row(s) in set (0.000943s)
```

### TWA

```
SELECT TWA(field_name) FROM tb_name WHERE clause;
```

81
**Description**:Time weighted average on a specific column within a time range
D
dingbo 已提交
82

83
**Return value type**:Double precision floating number
D
dingbo 已提交
84

85
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
86

87
**Applicable table types**:table, stable
D
dingbo 已提交
88

89
**More explanations**:
D
dingbo 已提交
90

91
- From version 2.1.3.0, function TWA can be used on stble with `GROUP BY`, i.e. timelines generated by `GROUP BY tbname` on a stable.
D
dingbo 已提交
92 93 94 95 96 97 98

### IRATE

```
SELECT IRATE(field_name) FROM tb_name WHERE clause;
```

99
**Description**:instantaneous rate on a specific column. The last two samples in the specified time range are used to calculate instantaneous rate. If the last sample value is smaller, then only the last sample value is used instead of the difference between the last two sample values.
D
dingbo 已提交
100

101
**Return value type**:Double precision floating number
D
dingbo 已提交
102

103
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
104

105
**Applicable table types**:table, stable
D
dingbo 已提交
106

107
**More explanations**:
D
dingbo 已提交
108

109
- From version 2.1.3.0, function IRATE can be used on stble with `GROUP BY`, i.e. timelines generated by `GROUP BY tbname` on a stable.
D
dingbo 已提交
110 111 112 113 114 115 116

### SUM

```
SELECT SUM(field_name) FROM tb_name [WHERE clause];
```

117
**Description**:The sum of a specific column in a table or stable
D
dingbo 已提交
118

119
**Return value type**:Double precision floating number or long integer
D
dingbo 已提交
120

121
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
122

123
**Applicable table types**:table, stable
D
dingbo 已提交
124

125
**Examples**:
D
dingbo 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

```
taos> SELECT SUM(current), SUM(voltage), SUM(phase) FROM meters;
    sum(current)        |     sum(voltage)      |        sum(phase)         |
================================================================================
            103.200000763 |                  1984 |               2.640000001 |
Query OK, 1 row(s) in set (0.001702s)

taos> SELECT SUM(current), SUM(voltage), SUM(phase) FROM d1001;
    sum(current)        |     sum(voltage)      |        sum(phase)         |
================================================================================
            35.200000763 |                   658 |               0.950000018 |
Query OK, 1 row(s) in set (0.000980s)
```

### STDDEV

```
SELECT STDDEV(field_name) FROM tb_name [WHERE clause];
```

147
**Description**:Standard deviation of a specific column in a table or stable
D
dingbo 已提交
148

149
**Return value type**:Double precision floating number
D
dingbo 已提交
150

151
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
152

153
**Applicable table types**:table, stable (starting from version 2.0.15.1)
D
dingbo 已提交
154

155
**Examples**:
D
dingbo 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

```
taos> SELECT STDDEV(current) FROM d1001;
    stddev(current)      |
============================
            1.020892909 |
Query OK, 1 row(s) in set (0.000915s)
```

### LEASTSQUARES

```
SELECT LEASTSQUARES(field_name, start_val, step_val) FROM tb_name [WHERE clause];
```

171
**Description**:统计表中某列的值是主键(时间戳)的拟合直线方程.start_val 是自变量初始值,step_val 是自变量的步长值.
D
dingbo 已提交
172

173
**Return value type**:A string in the format of "(slope, intercept)"
D
dingbo 已提交
174

175
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
176

177
**Applicable table types**:table only
D
dingbo 已提交
178

179
**Examples**:
D
dingbo 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

```
taos> SELECT LEASTSQUARES(current, 1, 1) FROM d1001;
            leastsquares(current, 1, 1)             |
=====================================================
{slop:1.000000, intercept:9.733334}                 |
Query OK, 1 row(s) in set (0.000921s)
```

### MODE

```
SELECT MODE(field_name) FROM tb_name [WHERE clause];
```

195
**Description**:The value which has the highest frequency of occurrence. NULL is returned if there are multiple values which have highest frequency of occurrence. It can't be used on timestamp column or tags.
D
dingbo 已提交
196

197
**Return value type**:Same as the data type of the column being operated
D
dingbo 已提交
198

199
**Applicable column types**:Data types except for timestamp
D
dingbo 已提交
200

201
**More explanations**:Considering the number of returned result set is unpredictable, it's suggested to limit the number of unique values to 100,000, otherwise error will be returned.
D
dingbo 已提交
202

203
**Applicable version**:From version 2.6.0.0
D
dingbo 已提交
204

205
**Examples**:
D
dingbo 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

```
taos> select voltage from d002;
    voltage        |
========================
       1           |
       1           |
       2           |
       19          |
Query OK, 4 row(s) in set (0.003545s)

taos> select mode(voltage) from d002;
  mode(voltage)    |
========================
       1           |
Query OK, 1 row(s) in set (0.019393s)
```

### HYPERLOGLOG

```
SELECT HYPERLOGLOG(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

230
**Description**:The cardinal number of a specific column is returned by using hyperloglog algorithm.
D
dingbo 已提交
231

232
**Return value type**:Integer
D
dingbo 已提交
233

234
**Applicable column types**:Any data type
D
dingbo 已提交
235

236
**More explanations**: The benefit of using hyperloglog algorithm is that the memory usage is under control when the data volume is huge. However, when the data volume is very small, the result may be not accurate, it's recommented to use `select count(data) from (select unique(col) as data from table)` in this case.
D
dingbo 已提交
237

238 239 240
**Applicable versions**:From version 2.6.0.0

**Examples**:
D
dingbo 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

```
taos> select dbig from shll;
     dbig          |
========================
       1           |
       1           |
       1           |
       NULL        |
       2           |
       19          |
       NULL        |
       9           |
Query OK, 8 row(s) in set (0.003755s)

taos> select hyperloglog(dbig) from shll;
  hyperloglog(dbig)|
========================
       4           |
Query OK, 1 row(s) in set (0.008388s)
```

263
## Selection Functions
D
dingbo 已提交
264

265
When any selective function is used, timestamp column or tag columns including `tbname` can be specified to show that the selected value are from which rows.
D
dingbo 已提交
266 267 268 269 270 271 272

### MIN

```
SELECT MIN(field_name) FROM {tb_name | stb_name} [WHERE clause];
```

273
**Description**:The minimum value of a specific column in a table or stable
D
dingbo 已提交
274

275
**Return value type**:Same as the data type of the column being operated
D
dingbo 已提交
276

277
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
278

279
**Applicable table types**:table, stable
D
dingbo 已提交
280

281
**Examples**:
D
dingbo 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

```
taos> SELECT MIN(current), MIN(voltage) FROM meters;
    min(current)     | min(voltage) |
======================================
            10.20000 |          218 |
Query OK, 1 row(s) in set (0.001765s)

taos> SELECT MIN(current), MIN(voltage) FROM d1001;
    min(current)     | min(voltage) |
======================================
            10.30000 |          218 |
Query OK, 1 row(s) in set (0.000950s)
```

### MAX

```
SELECT MAX(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

303
**Description**:The maximum value of a specific column of a table or stable
D
dingbo 已提交
304

305
**Return value type**:Same as the data type of the column being operated
D
dingbo 已提交
306

307
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
308

309
**Applicable table types**:table, stable
D
dingbo 已提交
310

311
**Examples**:
D
dingbo 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332

```
taos> SELECT MAX(current), MAX(voltage) FROM meters;
    max(current)     | max(voltage) |
======================================
            13.40000 |          223 |
Query OK, 1 row(s) in set (0.001123s)

taos> SELECT MAX(current), MAX(voltage) FROM d1001;
    max(current)     | max(voltage) |
======================================
            12.60000 |          221 |
Query OK, 1 row(s) in set (0.000987s)
```

### FIRST

```
SELECT FIRST(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

333
**Description**:The first non-null value of a specific column in a table or stable
D
dingbo 已提交
334

335
**Return value type**:Same as the column being operated
D
dingbo 已提交
336

337
**Applicable column types**:Any data type
D
dingbo 已提交
338

339
**Applicable table types**:table, stable
D
dingbo 已提交
340

341
**More explanations**:
D
dingbo 已提交
342

343 344 345
- FIRST(\*) can be used to get the first non-null value of all columns
- NULL will be returned if all the values of the specified column are all NULL
- No result will NOT be returned if all the columns in the result set are all NULL
D
dingbo 已提交
346

347
**Examples**:
D
dingbo 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

```
taos> SELECT FIRST(*) FROM meters;
        first(ts)        |    first(current)    | first(voltage) |     first(phase)     |
=========================================================================================
2018-10-03 14:38:04.000 |             10.20000 |            220 |              0.23000 |
Query OK, 1 row(s) in set (0.004767s)

taos> SELECT FIRST(current) FROM d1002;
    first(current)    |
=======================
            10.20000 |
Query OK, 1 row(s) in set (0.001023s)
```

### LAST

```
SELECT LAST(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

369
**Description**:The last non-NULL value of a specific column in a table or stable
D
dingbo 已提交
370

371
**Return value type**:Same as the column being operated
D
dingbo 已提交
372

373
**Applicable column types**:Any data type
D
dingbo 已提交
374

375
**Applicable table types**:table, stable
D
dingbo 已提交
376

377
**More explanations**:
D
dingbo 已提交
378

379 380 381
- LAST(\*) can be used to get the last non-NULL value of all columns
- If the values of a column in the result set are all NULL, NULL is returned for that column; if all columns in the result are all NULL, no result will be returned.
- When it's used on a stable, if there are multiple values with the timestamp in the result set, one of them will be returned randomly and it's not guaranteed that the same value is returned if the same query is run multiple times.
D
dingbo 已提交
382

383
**Examples**:
D
dingbo 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

```
taos> SELECT LAST(*) FROM meters;
        last(ts)         |    last(current)     | last(voltage) |     last(phase)      |
========================================================================================
2018-10-03 14:38:16.800 |             12.30000 |           221 |              0.31000 |
Query OK, 1 row(s) in set (0.001452s)

taos> SELECT LAST(current) FROM d1002;
    last(current)     |
=======================
            10.30000 |
Query OK, 1 row(s) in set (0.000843s)
```

### TOP

```
SELECT TOP(field_name, K) FROM { tb_name | stb_name } [WHERE clause];
```

405
**Description**: The greatest _k_ values of a specific column in a table or stable. If a value has multiple occurrences in the column but counting all of them in will exceed the upper limit _k_, then a part of them will be returned randomly.
D
dingbo 已提交
406

407
**Return value type**:Same as the column being operated
D
dingbo 已提交
408

409
**Applicable column types**:Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
410

411
**Applicable table types**:table, stable
D
dingbo 已提交
412

413
**More explanations**:
D
dingbo 已提交
414

415 416 417
- _k_ must be in range [1,100]
- The timestamp associated with the selected values are returned too
- Can't be used with `FILL`
D
dingbo 已提交
418

419
**Examples**:
D
dingbo 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443

```
taos> SELECT TOP(current, 3) FROM meters;
        ts            |   top(current, 3)    |
=================================================
2018-10-03 14:38:15.000 |             12.60000 |
2018-10-03 14:38:16.600 |             13.40000 |
2018-10-03 14:38:16.800 |             12.30000 |
Query OK, 3 row(s) in set (0.001548s)

taos> SELECT TOP(current, 2) FROM d1001;
        ts            |   top(current, 2)    |
=================================================
2018-10-03 14:38:15.000 |             12.60000 |
2018-10-03 14:38:16.800 |             12.30000 |
Query OK, 2 row(s) in set (0.000810s)
```

### BOTTOM

```
SELECT BOTTOM(field_name, K) FROM { tb_name | stb_name } [WHERE clause];
```

444
**Description**:The least _k_ values of a specific column in a table or stable. If a value has multiple occurrences in the column but counting all of them in will exceed the upper limit _k_, then a part of them will be returned randomly.
D
dingbo 已提交
445

446
**Return value type**:Same as the column being operated
D
dingbo 已提交
447

448
**Applicable column types**: Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
449

450
**Applicable table types**: table, stable
D
dingbo 已提交
451

452
**More explanations**:
D
dingbo 已提交
453

454 455 456
- _k_ must be in range [1,100]
- The timestamp associated with the selected values are returned too
- Can't be used with `FILL`
D
dingbo 已提交
457

458
**Examples**:
D
dingbo 已提交
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

```
taos> SELECT BOTTOM(voltage, 2) FROM meters;
        ts            | bottom(voltage, 2) |
===============================================
2018-10-03 14:38:15.000 |                218 |
2018-10-03 14:38:16.650 |                218 |
Query OK, 2 row(s) in set (0.001332s)

taos> SELECT BOTTOM(current, 2) FROM d1001;
        ts            |  bottom(current, 2)  |
=================================================
2018-10-03 14:38:05.000 |             10.30000 |
2018-10-03 14:38:16.800 |             12.30000 |
Query OK, 2 row(s) in set (0.000793s)
```

### PERCENTILE

```
SELECT PERCENTILE(field_name, P) FROM { tb_name } [WHERE clause];
```

482
**Description**: The value whose rank in a specific column matches the specified percentage. If such a value matching the specified percentage doesn't exist in the column, an interpolation value will be returned.
D
dingbo 已提交
483

484
**Return value type**: Double precision floating point
D
dingbo 已提交
485

486
**Applicable column types**: Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
487

488
**Applicable table types**: table
D
dingbo 已提交
489

490
**More explanations**: _P_ is in range [0,100], when _P_ is 0, the result is same as using function MIN; when _P_ is 100, the result is same as function MAX.
D
dingbo 已提交
491

492
**Examples**:
D
dingbo 已提交
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

```
taos> SELECT PERCENTILE(current, 20) FROM d1001;
percentile(current, 20)  |
============================
            11.100000191 |
Query OK, 1 row(s) in set (0.000787s)
```

### APERCENTILE

```
SELECT APERCENTILE(field_name, P[, algo_type])
FROM { tb_name | stb_name } [WHERE clause]
```

509
**Description**: Similar to `PERCENTILE`, but a simulated result is returned
D
dingbo 已提交
510

511
**Return value type**: Double precision floating point
D
dingbo 已提交
512

513
**Applicable column types**: Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
514

515
**Applicable table types**: table, stable
D
dingbo 已提交
516

517
**More explanations**
D
dingbo 已提交
518

519 520 521
- _P_ is in range [0,100], when _P_ is 0, the result is same as using function MIN; when _P_ is 100, the result is same as function MAX.
- **algo_type** can only be input as `default` or `t-digest`, if it's not specified `default` will be used, i.e. `apercentile(column_name, 50)` is same as `apercentile(column_name, 50, "default")`.
- When `t-digest` is used, `t-digest` sampling is used to calculate. It can be used from version 2.2.0.0.
D
dingbo 已提交
522

523
**Nested query**: It can be used in both the outer query and inner query in a nested query.
D
dingbo 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550

```
taos> SELECT APERCENTILE(current, 20) FROM d1001;
apercentile(current, 20)  |
============================
            10.300000191 |
Query OK, 1 row(s) in set (0.000645s)

taos> select apercentile (count, 80, 'default') from stb1;
 apercentile (c0, 80, 'default') |
==================================
             601920857.210056424 |
Query OK, 1 row(s) in set (0.012363s)

taos> select apercentile (count, 80, 't-digest') from stb1;
 apercentile (c0, 80, 't-digest') |
===================================
              605869120.966666579 |
Query OK, 1 row(s) in set (0.011639s)
```

### LAST_ROW

```
SELECT LAST_ROW(field_name) FROM { tb_name | stb_name };
```

551
**Description**: The last row of a table or stable
D
dingbo 已提交
552

553
**Return value type**: Same as the column being operated
D
dingbo 已提交
554

555
**Applicable column types**: Any data type
D
dingbo 已提交
556

557
**Applicable table types**: table, stable
D
dingbo 已提交
558

559
**More explanations**:
D
dingbo 已提交
560

561 562
- When it's used against a stable, multiple rows with the same and largest timestamp may exist, in this case one of them is returned randomly and it's not guaranteed that the result is same if the query is run multiple times.
- Can't be used with `INTERVAL`.
D
dingbo 已提交
563

564
**Examples**:
D
dingbo 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

```
 taos> SELECT LAST_ROW(current) FROM meters;
 last_row(current)   |
 =======================
             12.30000 |
 Query OK, 1 row(s) in set (0.001238s)

 taos> SELECT LAST_ROW(current) FROM d1002;
 last_row(current)   |
 =======================
             10.30000 |
 Query OK, 1 row(s) in set (0.001042s)
```

580
### INTERP [From version 2.3.1]
D
dingbo 已提交
581 582 583 584 585

```
SELECT INTERP(field_name) FROM { tb_name | stb_name } [WHERE where_condition] [ RANGE(timestamp1,timestamp2) ] [EVERY(interval)] [FILL ({ VALUE | PREV | NULL | LINEAR | NEXT})];
```

586
**Description**: The value that matches the specified timestamp range is returned, if existing; or an interpolation value is returned.
D
dingbo 已提交
587

588
**Return value type**: same as the column being operated
D
dingbo 已提交
589

590
**Applicable column types**: Numeric data types
D
dingbo 已提交
591

592
**Applicable table types**: table, stable, nested query
D
dingbo 已提交
593

594
**More explanations**
D
dingbo 已提交
595

596 597 598 599 600 601 602
- `INTERP` is used to get the value that matches the specified time slice from a column. If no such value exists an interpolation value will be returned based on `FILL` parameter.
- The input data of `INTERP` is the value of the specified column, `where` can be used to filter the original data. If no `where` condition is specified then all original data is the input.
- The output time range of `INTERP` is specified by `RANGE(timestamp1,timestamp2)` parameter, with timestamp1<=timestamp2. timestamp1 is the starting point of the output time range and must be specified. timestamp2 is the ending point of the output time range and must be specified. If `RANGE` is not specified, then the timestamp of the first row that matches the filter condition is treated as timestamp1, the timestamp of the last row that matches the filter condition is treated as timestamp2.
- The number of rows in the result set of `INTERP` is determined by the parameter `EVERY`. Starting from timestamp1, one interpolation is performed for every time interval specified `EVERY` parameter. If `EVERY` parameter is not used, the time windows will be considered as no ending timestamp, i.e. there is only one time window from timestamp1.
- Interpolation is performed based on `FILL` parameter. No interpolation is performed if `FILL` is not used, that means either the original data that matches is returned or nothing is returned.
- `INTERP` can only be used to interpolate in single timeline. So it must be used with `group by tbname` when it's used on a stable. It can't be used with `GROUP BY` when it's used in the inner query of a nested query.
- The result of `INTERP` is not influenced by `ORDER BY TIMESTAMP`, which impacts the output order only..
D
dingbo 已提交
603

604
**Examples**: Based on the `meters` schema used throughout the documents
D
dingbo 已提交
605

606
- Single point linear interpolation between "2017-07-14 18:40:00" and "2017-07-14 18:40:00:
D
dingbo 已提交
607 608 609 610 611

```
 taos> SELECT INTERP(current) FROM t1 RANGE('2017-7-14 18:40:00','2017-7-14 18:40:00') FILL(LINEAR);
```

612
- Get an original data every 5 seconds, no interpolation, between "2017-07-14 18:00:00" and "2017-07-14 19:00:00:
D
dingbo 已提交
613 614 615 616 617

```
 taos> SELECT INTERP(current) FROM t1 RANGE('2017-7-14 18:00:00','2017-7-14 19:00:00') EVERY(5s);
```

618
- Linear interpolation every 5 seconds between "2017-07-14 18:00:00" and "2017-07-14 19:00:00:
D
dingbo 已提交
619 620 621 622 623

```
  taos> SELECT INTERP(current) FROM t1 RANGE('2017-7-14 18:00:00','2017-7-14 19:00:00') EVERY(5s) FILL(LINEAR);
```

624
- Backward interpolation every 5 seconds
D
dingbo 已提交
625 626 627 628 629

```
  taos> SELECT INTERP(current) FROM t1 EVERY(5s) FILL(NEXT);
```

630
- Linear interpolation every 5 seconds between "2017-07-14 17:00:00" and "2017-07-14 20:00:00"
D
dingbo 已提交
631 632

```
633
  taos> SELECT INTERP(current) FROM t1 where ts >= '2017-07-14 17:00:00' and ts <= '2017-07-14 20:00:00' RANGE('2017-7-14 18:00:00','2017-7-14 19:00:00') EVERY(5s) FILL(LINEAR);
D
dingbo 已提交
634 635
```

636
### INTERP [Prior to version 2.3.1]
D
dingbo 已提交
637 638 639 640 641

```
SELECT INTERP(field_name) FROM { tb_name | stb_name } WHERE ts='timestamp' [FILL ({ VALUE | PREV | NULL | LINEAR | NEXT})];
```

642
**Description**: The value of a specific column that matches the specified time slice
D
dingbo 已提交
643

644
**Return value type**: Same as the column being operated
D
dingbo 已提交
645

646
**Applicable column types**: Numeric data type
D
dingbo 已提交
647

648
**Applicable table types**: table, stable
D
dingbo 已提交
649

650
**More explanations**:
D
dingbo 已提交
651

652 653 654 655 656
- It can be used from version 2.0.15.0
- Time slice must be specified. If there is no data matching the specified time slice, interpolation is performed based on `FILL` parameter. Conditions such as tags or `tbname` can be used `Where` clause can be used to filter data.
- The timestamp specified must be within the time range of the data rows of the table or stable. If it is beyond the valid time range, nothing is returned even with `FILL` parameter.
- `INTERP` can be used to query only single time point once. `INTERP` can be used with `EVERY` to get the interpolation value every time interval.
- **Examples**:
D
dingbo 已提交
657 658 659 660 661 662 663 664 665

```
 taos> SELECT INTERP(*) FROM meters WHERE ts='2017-7-14 18:40:00.004';
        interp(ts)        |   interp(current)    | interp(voltage) |    interp(phase)     |
 ==========================================================================================
  2017-07-14 18:40:00.004 |              9.84020 |             216 |              0.32222 |
 Query OK, 1 row(s) in set (0.002652s)
```

666
If there is not any data corresponding to the specified timestamp, an interpolation value is returned if interpolation policy is specified by `FILL` parameter; or nothing is returned\
D
dingbo 已提交
667 668 669 670 671 672 673 674 675 676 677 678

```
 taos> SELECT INTERP(*) FROM meters WHERE tbname IN ('d636') AND ts='2017-7-14 18:40:00.005';
 Query OK, 0 row(s) in set (0.004022s)

 taos> SELECT INTERP(*) FROM meters WHERE tbname IN ('d636') AND ts='2017-7-14 18:40:00.005' FILL(PREV);
        interp(ts)        |   interp(current)    | interp(voltage) |    interp(phase)     |
 ==========================================================================================
  2017-07-14 18:40:00.005 |              9.88150 |             217 |              0.32500 |
 Query OK, 1 row(s) in set (0.003056s)
```

679
Interpolation is performed every 5 milliseconds between `['2017-7-14 18:40:00', '2017-7-14 18:40:00.014']`
D
dingbo 已提交
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

```
 taos> SELECT INTERP(current) FROM d636 WHERE ts>='2017-7-14 18:40:00' AND ts<='2017-7-14 18:40:00.014' EVERY(5a);
            ts            |   interp(current)    |
 =================================================
  2017-07-14 18:40:00.000 |             10.04179 |
  2017-07-14 18:40:00.010 |             10.16123 |
 Query OK, 2 row(s) in set (0.003487s)
```

### TAIL

```
SELECT TAIL(field_name, k, offset_val) FROM {tb_name | stb_name} [WHERE clause];
```

696
**Description**: The next _k_ rows are returned after skipping the last `offset_val` rows, NULL values are not ignored. `offset_val` is optional parameter. When it's not specified, the last _k_ rows are returned. When `offset_val` is used, the effect is same as `order by ts desc LIMIT k OFFSET offset_val`.
D
dingbo 已提交
697

698
**Parameter value range**: k: [1,100] offset_val: [0,100]
D
dingbo 已提交
699

700
**Return value type**: Same as the column being operated
D
dingbo 已提交
701

702
**Applicable column types**: Any data type except form timestamp, i.e. the primary key
D
dingbo 已提交
703

704
**Applicable versions**: From version 2.6.0.0
D
dingbo 已提交
705

706
**Examples**:
D
dingbo 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

```
taos> select ts,dbig from tail2;
       ts            |         dbig          |
==================================================
2021-10-15 00:31:33.000 |                     1 |
2021-10-17 00:31:31.000 |                  NULL |
2021-12-24 00:31:34.000 |                     2 |
2022-01-01 08:00:05.000 |                    19 |
2022-01-01 08:00:06.000 |                  NULL |
2022-01-01 08:00:07.000 |                     9 |
Query OK, 6 row(s) in set (0.001952s)

taos> select tail(dbig,2,2) from tail2;
ts                      |    tail(dbig,2,2)     |
==================================================
2021-12-24 00:31:34.000 |                     2 |
2022-01-01 08:00:05.000 |                    19 |
Query OK, 2 row(s) in set (0.002307s)
```

### UNIQUE

```
SELECT UNIQUE(field_name) FROM {tb_name | stb_name} [WHERE clause];
```

734
**Description**: The values that occur the first time in the specified column. The effect is similar to `distinct` keyword, but it can also be used to match tags or timestamp.
D
dingbo 已提交
735

736
**Return value type**: Same as the column or tag being operated
D
dingbo 已提交
737

738
**Applicable column types**: Any data types except for timestamp
D
dingbo 已提交
739

740
**支持版本**: From version 2.6.0.0
D
dingbo 已提交
741

742
**More explanations**:
D
dingbo 已提交
743

744 745
- It can be used against table or stable, but can't be used together with time window, like `interval`, `state_window` or `session_window` .
- Considering the number of result sets is unpredictable, it's suggested to limit the distinct values under 100,000 to control the memory usage, otherwise error will be returned.
D
dingbo 已提交
746

747
**Examples**:
D
dingbo 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773

```
taos> select ts,voltage from unique1;
       ts            |        voltage        |
==================================================
2021-10-17 00:31:31.000 |                     1 |
2022-01-24 00:31:31.000 |                     1 |
2021-10-17 00:31:31.000 |                     1 |
2021-12-24 00:31:31.000 |                     2 |
2022-01-01 08:00:01.000 |                    19 |
2021-10-17 00:31:31.000 |                  NULL |
2022-01-01 08:00:02.000 |                  NULL |
2022-01-01 08:00:03.000 |                     9 |
Query OK, 8 row(s) in set (0.003018s)

taos> select unique(voltage) from unique1;
ts                      |    unique(voltage)    |
==================================================
2021-10-17 00:31:31.000 |                     1 |
2021-10-17 00:31:31.000 |                  NULL |
2021-12-24 00:31:31.000 |                     2 |
2022-01-01 08:00:01.000 |                    19 |
2022-01-01 08:00:03.000 |                     9 |
Query OK, 5 row(s) in set (0.108458s)
```

774
## Scalar functions
D
dingbo 已提交
775 776 777

### DIFF

778 779 780
```sql
SELECT {DIFF(field_name, ignore_negative) | DIFF(field_name)} FROM tb_name [WHERE clause];
```
D
dingbo 已提交
781

782
**Description**: The different of each row with its previous row for a specific column. `ignore_negative` can be specified as 0 or 1, the default value is 1 if it's not specified. `1` means negative values are ignored.
D
dingbo 已提交
783

784
**Return value type**: Same as the column being operated
D
dingbo 已提交
785

786
**Applicable column types**: Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
787

788
**Applicable table types**: table, stable
D
dingbo 已提交
789

790
**More explanations**:
D
dingbo 已提交
791

792 793 794
- The number of result rows is the number of rows subtracted by one, no output for the first row
- From version 2.1.30, `DIFF` can be used on stable with `GROUP by tbname`
- From version 2.6.0, `ignore_negative` parameter is supported
D
dingbo 已提交
795

796
**Examples**:
D
dingbo 已提交
797

798 799 800 801 802 803 804 805
```sql
taos> SELECT DIFF(current) FROM d1001;
        ts            |    diff(current)     |
=================================================
2018-10-03 14:38:15.000 |              2.30000 |
2018-10-03 14:38:16.800 |             -0.30000 |
Query OK, 2 row(s) in set (0.001162s)
```
D
dingbo 已提交
806 807 808 809 810 811 812

### DERIVATIVE

```
SELECT DERIVATIVE(field_name, time_interval, ignore_negative) FROM tb_name [WHERE clause];
```

813
**Description**: The derivative of a specific column. The time rage can be specified by parameter `time_interval` 参数指定, the minimum allowed time range is 1 second (1s); the value of `ignore_negative` can be 0 or 1, 1 means negative values are ignored.
D
dingbo 已提交
814

815
**Return value type**: Double precision floating point
D
dingbo 已提交
816

817
**Applicable column types**: Data types except for timestamp, binary, nchar and bool
D
dingbo 已提交
818

819
**Applicable table types**: table, stable
D
dingbo 已提交
820

821
**More explanations**:
D
dingbo 已提交
822

823 824
- It is available from version 2.1.3.0, the number of result rows is the number of total rows in the time range subtracted by one, no output for the first row.\
- It can be used together with `GROUP BY tbname` against a stable.
D
dingbo 已提交
825

826
**Examples**:
D
dingbo 已提交
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845

```
taos> select derivative(current, 10m, 0) from t1;
           ts            | derivative(current, 10m, 0) |
========================================================
 2021-08-20 10:11:22.790 |                 0.500000000 |
 2021-08-20 11:11:22.791 |                 0.166666620 |
 2021-08-20 12:11:22.791 |                 0.000000000 |
 2021-08-20 13:11:22.792 |                 0.166666620 |
 2021-08-20 14:11:22.792 |                -0.666666667 |
Query OK, 5 row(s) in set (0.004883s)
```

### SPREAD

```
SELECT SPREAD(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

846
**Description**: The difference between the max and the min of a specific column
D
dingbo 已提交
847

848
**Return value type**: Double precision floating point
D
dingbo 已提交
849

850
**Applicable column types**: Data types except for binary, nchar, and bool
D
dingbo 已提交
851

852
**Applicable table types**: table, stable
D
dingbo 已提交
853

854
**More explanations**: Can be used on a column of TIMESTAMP type, the result is the time range size.可
D
dingbo 已提交
855

856
**Examples**:
D
dingbo 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

```
taos> SELECT SPREAD(voltage) FROM meters;
    spread(voltage)      |
============================
            5.000000000 |
Query OK, 1 row(s) in set (0.001792s)

taos> SELECT SPREAD(voltage) FROM d1001;
    spread(voltage)      |
============================
            3.000000000 |
Query OK, 1 row(s) in set (0.000836s)
```

### CEIL

```
SELECT CEIL(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

878
**Description**: The round up value of a specific column
D
dingbo 已提交
879

880
**Return value type**: Same as the column being used
D
dingbo 已提交
881

882
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
883

884
**Applicable table types**: table, stable
D
dingbo 已提交
885

886
**Applicable nested query**: inner query and outer query
D
dingbo 已提交
887

888
**More explanations**:
D
dingbo 已提交
889

890 891 892
- Can't be used on any tags of any type
- Arithmetic operation can be performed on the result of `ceil` function
- Can't be used with aggregate functions
D
dingbo 已提交
893 894 895 896 897 898 899

### FLOOR

```
SELECT FLOOR(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

900 901 902
**Description**: The round down value of a specific column

**More explanations**: The restrictions are same as `CEIL` function.
D
dingbo 已提交
903 904 905 906 907 908 909

### ROUND

```
SELECT ROUND(field_name) FROM { tb_name | stb_name } [WHERE clause];
```

910 911 912
**Description**: The round value of a specific column.

**More explanations**: The restrictions are same as `CEIL` function.
D
dingbo 已提交
913 914 915 916 917 918 919

### CSUM

    ```sql
    SELECT CSUM(field_name) FROM { tb_name | stb_name } [WHERE clause]
    ```

920
**Description**: The cumulative sum of each row for a specific column. The number of output rows is same as that of the input rows.
D
dingbo 已提交
921

922
**Return value type**: Long integer for integers; Double for floating points. Timestamp is returned for each row.
D
dingbo 已提交
923

924
**Applicable data types**: Data types except for timestamp, binary, nchar, and bool
D
dingbo 已提交
925

926
**Applicable table types**: table, stable
D
dingbo 已提交
927

928
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
929

930 931 932 933 934 935 936 937
**More explanations**:

- Can't be used on tags when it's used on stable
- Arithmetic operation can't be performed on the result of `csum` function
- Can only be used with aggregate functions
- `Group by tbname` must be used together on a stable to force the result on a single timeline

**Applicable versions**: From 2.3.0.x
D
dingbo 已提交
938 939 940 941 942 943 944

### MAVG

    ```sql
    SELECT MAVG(field_name, K) FROM { tb_name | stb_name } [WHERE clause]
    ```

945 946 947
**Description**: The moving average of continuous _k_ values of a specific column. If the number of input rows is less than _k_, nothing is returned. The applicable range is _k_ is [1,1000].

**Return value type**: Double precision floating point
D
dingbo 已提交
948

949
**Applicable data types**: Data types except for timestamp, binary, nchar, and bool
D
dingbo 已提交
950

951
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
952

953
**Applicable table types**: table, stable
D
dingbo 已提交
954

955
**More explanations**:
D
dingbo 已提交
956

957 958 959 960 961 962
- Arithmetic operation can't be performed on the result of `MAVG`.
- Can only be used with data columns, can't be used with tags.
- Can't be used with aggregate functions.\(Aggregation)函数一起使用;
- Must be used with `GROUP BY tbname` when it's used on a stable to force the result on each single timeline.该

**Applicable versions**: From 2.3.0.x
D
dingbo 已提交
963 964 965 966 967 968 969

### SAMPLE

    ```sql
    SELECT SAMPLE(field_name, K) FROM { tb_name | stb_name } [WHERE clause]
    ```

970 971 972
**Description**: _k_ sampling values of a specific column. The applicable range of _k_ is [1,10000]

**Return value type**: Same as the column being operated plus the associated timestamp
D
dingbo 已提交
973

974
**Applicable data types**: Any data type except for tags of stable
D
dingbo 已提交
975

976
**Applicable table types**: table, stable
D
dingbo 已提交
977

978
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
979

980
**More explanations**:
D
dingbo 已提交
981

982 983 984 985
- Arithmetic operation can't be operated on the result of `SAMPLE` function
- Must be used with `Group by tbname` when it's used on a stable to force the result on each single timeline

**Applicable versions**: From 2.3.0.x
D
dingbo 已提交
986 987 988

### ASIN

989 990 991 992 993 994 995
```sql
SELECT ASIN(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The anti-sine of a specific column

**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
996

997
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
998

999
**Applicable table types**: table, stable
D
dingbo 已提交
1000

1001
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1002

1003
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1004

1005
**More explanations**:
D
dingbo 已提交
1006

1007 1008
- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1009 1010 1011

### ACOS

1012 1013 1014 1015 1016 1017 1018
```sql
SELECT ACOS(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The anti-cosine of a specific column 获

**Return value type**: ouble if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1019

1020
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1021

1022
**Applicable table types**: table, stable
D
dingbo 已提交
1023

1024
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1025

1026
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1027

1028
**More explanations**:
D
dingbo 已提交
1029

1030 1031
- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1032 1033 1034

### ATAN

1035 1036 1037 1038 1039
```sql
SELECT ATAN(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: anti-tangent of a specific column
D
dingbo 已提交
1040

1041
**Description**: The anti-cosine of a specific column 获
D
dingbo 已提交
1042

1043
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1044

1045
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1046

1047
**Applicable table types**: table, stable
D
dingbo 已提交
1048

1049
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1050

1051 1052 1053 1054 1055 1056
**Applicable versions**: From 2.6.0.0

**More explanations**:

- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1057 1058 1059

### SIN

1060 1061 1062 1063 1064 1065 1066 1067 1068
```sql
SELECT SIN(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The sine of a specific column

**Description**: The anti-cosine of a specific column 获

**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1069

1070
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1071

1072
**Applicable table types**: table, stable
D
dingbo 已提交
1073

1074
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1075

1076
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1077

1078
**More explanations**:
D
dingbo 已提交
1079

1080 1081
- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1082 1083 1084

### COS

1085 1086 1087 1088 1089 1090 1091
```sql
SELECT COS(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The cosine of a specific column

**Description**: The anti-cosine of a specific column 获
D
dingbo 已提交
1092

1093
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1094

1095
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1096

1097
**Applicable table types**: table, stable
D
dingbo 已提交
1098

1099
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1100

1101
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1102

1103 1104 1105 1106
**More explanations**:

- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1107 1108 1109

### TAN

1110 1111 1112 1113 1114 1115 1116
```sql
SELECT TAN(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The tangent of a specific column

**Description**: The anti-cosine of a specific column 获
D
dingbo 已提交
1117

1118
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1119

1120
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1121

1122
**Applicable table types**: table, stable
D
dingbo 已提交
1123

1124
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1125

1126
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1127

1128 1129 1130 1131
**More explanations**:

- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1132 1133 1134

### POW

1135 1136 1137 1138 1139 1140 1141
```sql
SELECT POW(field_name, power) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The power of a specific column with `power` as the index

**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1142

1143
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1144

1145
**Applicable table types**: table, stable
D
dingbo 已提交
1146

1147
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1148

1149
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1150

1151
**More explanations**:
D
dingbo 已提交
1152

1153 1154
- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1155 1156 1157

### LOG

1158 1159 1160 1161 1162 1163 1164
```sql
SELECT LOG(field_name, base) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The log of a specific with `base` as the radix

**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1165

1166
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1167

1168
**Applicable table types**: table, stable
D
dingbo 已提交
1169

1170
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1171

1172
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1173

1174
**More explanations**:
D
dingbo 已提交
1175

1176 1177
- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1178 1179 1180

### ABS

1181 1182 1183 1184 1185
```sql
SELECT ABS(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The absolute of a specific column
D
dingbo 已提交
1186

1187
**Return value type**: UBIGINT if the input value is integer; DOUBLE if the input value is FLOAT/DOUBLE 如
D
dingbo 已提交
1188

1189
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1190

1191
**Applicable table types**: table, stable
D
dingbo 已提交
1192

1193
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1194

1195
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1196

1197 1198 1199 1200
**More explanations**:

- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1201 1202 1203

### SQRT

1204 1205 1206 1207 1208
```sql
SELECT SQRT(field_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The square root of a specific column
D
dingbo 已提交
1209

1210
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
D
dingbo 已提交
1211

1212
**Applicable data types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1213

1214
**Applicable table types**: table, stable
D
dingbo 已提交
1215

1216
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1217

1218
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1219

1220 1221 1222 1223
**More explanations**:

- Can't be used with tags
- Can't be used with aggregate functions
D
dingbo 已提交
1224 1225 1226

### CAST

1227 1228 1229 1230 1231
```sql
SELECT CAST(expression AS type_name) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: It's used for type casting. The input parameter `expression` can be data columns, constants, scalar functions or arithmetic between them. Can't be used with tags, and can only be used in `select` clause.
D
dingbo 已提交
1232

1233
**Return value type**: The type specified by parameter `type_name`
D
dingbo 已提交
1234

1235
**Applicable data types**:
D
dingbo 已提交
1236

1237 1238
- Parameter `expression` can be any data type except for JSON, more specifically it can be any of BOOL/TINYINT/SMALLINT/INT/BIGINT/FLOAT/DOUBLE/BINARY(M)/TIMESTAMP/NCHAR(M)/TINYINT UNSIGNED/SMALLINT UNSIGNED/INT UNSIGNED/BIGINT UNSIGNED
- The output data type specified by `type_name` can only be one of BIGINT/BINARY(N)/TIMESTAMP/NCHAR(N)/BIGINT UNSIGNED
D
dingbo 已提交
1239

1240
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1241

1242
**More explanations**:
D
dingbo 已提交
1243

1244 1245 1246 1247 1248 1249 1250
- Error will be reported for unsupported type casting
- NULL will be returned if the input value is NULL
- Some values of some supported data types may not be casted, below are known issues:
  1)When casting BINARY/NCHAR to BIGINT/BIGINT UNSIGNED, some characters may be treated as illegal, for example "a" may be converted to 0.
  2)There may be overflow when casting singed integer or TIMESTAMP to unsigned BIGINT
  3)There may be overflow when casting unsigned BIGINT to BIGINT
  4)There may be overflow when casting FLOAT/DOUBLE to BIGINT or UNSIGNED BIGINT
D
dingbo 已提交
1251 1252 1253

### CONCAT

1254 1255 1256 1257 1258
```sql
SELECT CONCAT(str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The concatenation result of two or more strings, the number of strings to be concatenated is at least 2 and at most 8
D
dingbo 已提交
1259

1260
**Return value type**: Same as the columns being operated, BINARY or NCHAR; or NULL if all the input are NULL
D
dingbo 已提交
1261

1262
**Applicable data types**: The input data must be in either all BINARY or in all NCHAR; can't be used on tag columns
D
dingbo 已提交
1263

1264
**Applicable table types**: table, stable
D
dingbo 已提交
1265

1266
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1267

1268
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1269 1270 1271

### CONCAT_WS

1272 1273 1274 1275 1276
```
SELECT CONCAT_WS(separator, str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The concatenation result of two or more strings with separator, the number of strings to be concatenated is at least 3 and at most 9
D
dingbo 已提交
1277

1278
**Return value type**: Same as the columns being operated, BINARY or NCHAR; or NULL if all the input are NULL
D
dingbo 已提交
1279

1280
**Applicable data types**: The input data must be in either all BINARY or in all NCHAR; can't be used on tag columns
D
dingbo 已提交
1281

1282
**Applicable table types**: table, stable
D
dingbo 已提交
1283

1284
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1285

1286 1287 1288 1289 1290
**Applicable versions**: From 2.6.0.0

**More explanations**:

- If the value of `separator` is NULL, the output is NULL. If the value of `separator` is not NULL but other input are all NULL, the output is empty string.
D
dingbo 已提交
1291 1292 1293

### LENGTH

1294 1295 1296 1297 1298 1299 1300 1301 1302
```
SELECT LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The length in bytes of a string

**Return value type**: Integer

**Applicable data types**: BINARY or NCHAR, can't be used on tags
D
dingbo 已提交
1303

1304
**Applicable table types**: table, stable
D
dingbo 已提交
1305

1306
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1307

1308
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1309

1310
**More explanations**
D
dingbo 已提交
1311

1312
- If the input value is NULL, the output is NULL too
D
dingbo 已提交
1313 1314 1315

### CHAR_LENGTH

1316 1317 1318 1319 1320 1321 1322
```
SELECT CHAR_LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The length in number of characters of a string

**Return value type**: Integer
D
dingbo 已提交
1323

1324
**Applicable data types**: BINARY or NCHAR, can't be used on tags
D
dingbo 已提交
1325

1326
**Applicable table types**: table, stable
D
dingbo 已提交
1327

1328
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1329

1330
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1331

1332 1333 1334
**More explanations**

- If the input value is NULL, the output is NULL too
D
dingbo 已提交
1335 1336 1337

### LOWER

1338 1339 1340 1341 1342 1343 1344
```
SELECT LOWER(str|column) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: Convert the input string to lower case

**Return value type**: Same as input
D
dingbo 已提交
1345

1346
**Applicable data types**: BINARY or NCHAR, can't be used on tags
D
dingbo 已提交
1347

1348
**Applicable table types**: table, stable
D
dingbo 已提交
1349

1350
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1351

1352
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1353

1354 1355 1356
**More explanations**

- If the input value is NULL, the output is NULL too
D
dingbo 已提交
1357 1358 1359

### UPPER

1360 1361 1362 1363 1364 1365 1366
```
SELECT UPPER(str|column) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: Convert the input string to upper case

**Return value type**: Same as input
D
dingbo 已提交
1367

1368
**Applicable data types**: BINARY or NCHAR, can't be used on tags
D
dingbo 已提交
1369

1370
**Applicable table types**: table, stable
D
dingbo 已提交
1371

1372
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1373

1374
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1375

1376 1377 1378
**More explanations**

- If the input value is NULL, the output is NULL too
D
dingbo 已提交
1379 1380 1381

### LTRIM

1382 1383 1384 1385 1386
```
SELECT LTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: Remove the left leading blanks of a string
D
dingbo 已提交
1387

1388
**Return value type**: Same as input
D
dingbo 已提交
1389

1390
**Applicable data types**: BINARY or NCHAR, can't be used on tags
D
dingbo 已提交
1391

1392
**Applicable table types**: table, stable
D
dingbo 已提交
1393

1394
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1395

1396 1397 1398 1399 1400
**Applicable versions**: From 2.6.0.0

**More explanations**

- If the input value is NULL, the output is NULL too
D
dingbo 已提交
1401 1402 1403

### RTRIM

1404 1405 1406 1407 1408 1409 1410 1411 1412
```
SELECT RTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: Remove the right tailing blanks of a string

**Return value type**: Same as input

**Applicable data types**: BINARY or NCHAR, can't be used on tags
D
dingbo 已提交
1413

1414
**Applicable table types**: table, stable
D
dingbo 已提交
1415

1416
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1417

1418
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1419

1420
**More explanations**
D
dingbo 已提交
1421

1422
- If the input value is NULL, the output is NULL too
D
dingbo 已提交
1423 1424 1425

### SUBSTR

1426 1427 1428 1429 1430
```
SELECT SUBSTR(str,pos[,len]) FROM { tb_name | stb_name } [WHERE clause]
```

**Description**: The sub-string starting from `pos` with length of `len` from the original string `str`
D
dingbo 已提交
1431

1432
**Return value type**: Same as input
D
dingbo 已提交
1433

1434
**Applicable data types**: BINARY or NCHAR, can't be used on tags
D
dingbo 已提交
1435

1436
**Applicable table types**: table, stable
D
dingbo 已提交
1437

1438
**Applicable nested query**: Inner query and Outer query
D
dingbo 已提交
1439

1440
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1441

1442 1443 1444 1445 1446 1447 1448
**More explanations**:

- If the input is NULL, the output is NULL
- Parameter `pos` can be an positive or negative integer; If it's positive, the starting position will be counted from the beginning of the string; if it's negative, the starting position will be counted from the end of the string.
- If `len` is not specified, it means from `pos` to the end.

### Arithmetic Operations
D
dingbo 已提交
1449 1450 1451 1452 1453

```
SELECT field_name [+|-|*|/|%][Value|field_name] FROM { tb_name | stb_name }  [WHERE clause];
```

1454
**Description**: The sum, difference, product, quotient, or remainder between one or more columns
D
dingbo 已提交
1455

1456
**Return value type**: Double precision floating point
D
dingbo 已提交
1457

1458
**Applicable column types**: Data types except for timestamp, binary, nchar, bool
D
dingbo 已提交
1459

1460
**Applicable table types**: table, stable
D
dingbo 已提交
1461

1462
**More explanations**:
D
dingbo 已提交
1463

1464 1465 1466 1467
- Arithmetic operations can be performed on two or more columns, `()` can be used to control the precedence
- NULL doesn't participate the operation, if one of the operands is NULL then result is NULL

**Examples**:
D
dingbo 已提交
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484

```
taos> SELECT current + voltage * phase FROM d1001;
(current+(voltage*phase)) |
============================
            78.190000713 |
            84.540003240 |
            80.810000718 |
Query OK, 3 row(s) in set (0.001046s)
```

### STATECOUNT

```
SELECT STATECOUNT(field_name, oper, val) FROM { tb_name | stb_name } [WHERE clause];
```

1485 1486 1487
**Description**: The number of continuous rows satisfying the specified conditions for a specific column. The result is shown as an extra column for each row. If the specified condition is evaluated as true, the number is increased by 1; otherwise the number is reset to -1. If the input value is NULL, then the corresponding row is skipped.

**Applicable parameter values**:
D
dingbo 已提交
1488

1489 1490
- oper : Can be one of LT (lower than), GT (greater than), LE (lower than or euqal to), GE (greater than or equal to), NE (not equal to), EQ (equal to), the value is case insensitive
- val : Numeric types
D
dingbo 已提交
1491

1492
**Return value type**: Integer
D
dingbo 已提交
1493

1494
**Applicable data types**: Data types excpet for timestamp, binary, nchar, bool
D
dingbo 已提交
1495

1496
**Applicable table types**: table, stable
D
dingbo 已提交
1497

1498
**Applicable nested query**: Outer query only
D
dingbo 已提交
1499

1500
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1501

1502
**More explanations**:
D
dingbo 已提交
1503

1504 1505
- Must be used together with `GROUP BY tbname` when it's used on a stable to force the result into each single timeline]
- Can't be used with window operation, like interval/state_window/session_window
D
dingbo 已提交
1506

1507
**Examples**:
D
dingbo 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538

```
taos> select ts,dbig from statef2;
          ts               |         dbig          |
========================================================
2021-10-15 00:31:33.000000000 |                     1 |
2021-10-17 00:31:31.000000000 |                  NULL |
2021-12-24 00:31:34.000000000 |                     2 |
2022-01-01 08:00:05.000000000 |                    19 |
2022-01-01 08:00:06.000000000 |                  NULL |
2022-01-01 08:00:07.000000000 |                     9 |
Query OK, 6 row(s) in set (0.002977s)

taos> select stateCount(dbig,GT,2) from statef2;
ts               |         dbig          | statecount(dbig,gt,2) |
================================================================================
2021-10-15 00:31:33.000000000 |                     1 |                    -1 |
2021-10-17 00:31:31.000000000 |                  NULL |                  NULL |
2021-12-24 00:31:34.000000000 |                     2 |                    -1 |
2022-01-01 08:00:05.000000000 |                    19 |                     1 |
2022-01-01 08:00:06.000000000 |                  NULL |                  NULL |
2022-01-01 08:00:07.000000000 |                     9 |                     2 |
Query OK, 6 row(s) in set (0.002791s)
```

### STATEDURATION

```
SELECT stateDuration(field_name, oper, val, unit) FROM { tb_name | stb_name } [WHERE clause];
```

1539
**Description**: The length of time range in which all rows satisfy the specified condition for a specific column. The result is shown as an extra column for each row. The length for the first row that satisfies the condition is 0. Next, if the condition is evaluated as true for a row, the time interval between current row and its previous row is added up to the time range; otherwise the time range length is reset to -1. If the value of the column is NULL, the corresponding row is skipped.
D
dingbo 已提交
1540

1541
**Applicable parameter values**:
D
dingbo 已提交
1542

1543 1544 1545
- oper : Can be one of LT (lower than), GT (greater than), LE (lower than or euqal to), GE (greater than or equal to), NE (not equal to), EQ (equal to), the value is case insensitive
- val : Numeric types
- unit: The unit of time interval, can be [1s, 1m, 1h], default is 1s
D
dingbo 已提交
1546

1547
**Return value type**: Integer
D
dingbo 已提交
1548

1549
**Applicable data types**: Data types excpet for timestamp, binary, nchar, bool
D
dingbo 已提交
1550

1551
**Applicable table types**: table, stable
D
dingbo 已提交
1552

1553
**Applicable nested query**: Outer query only
D
dingbo 已提交
1554

1555
**Applicable versions**: From 2.6.0.0
D
dingbo 已提交
1556

1557
**More explanations**:
D
dingbo 已提交
1558

1559 1560 1561 1562
- Must be used together with `GROUP BY tbname` when it's used on a stable to force the result into each single timeline]
- Can't be used with window operation, like interval/state_window/session_window

**Examples**:
D
dingbo 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587

```
taos> select ts,dbig from statef2;
          ts               |         dbig          |
========================================================
2021-10-15 00:31:33.000000000 |                     1 |
2021-10-17 00:31:31.000000000 |                  NULL |
2021-12-24 00:31:34.000000000 |                     2 |
2022-01-01 08:00:05.000000000 |                    19 |
2022-01-01 08:00:06.000000000 |                  NULL |
2022-01-01 08:00:07.000000000 |                     9 |
Query OK, 6 row(s) in set (0.002407s)

taos> select stateDuration(dbig,GT,2) from statef2;
ts               |         dbig          | stateduration(dbig,gt,2) |
===================================================================================
2021-10-15 00:31:33.000000000 |                     1 |                       -1 |
2021-10-17 00:31:31.000000000 |                  NULL |                     NULL |
2021-12-24 00:31:34.000000000 |                     2 |                       -1 |
2022-01-01 08:00:05.000000000 |                    19 |                        0 |
2022-01-01 08:00:06.000000000 |                  NULL |                     NULL |
2022-01-01 08:00:07.000000000 |                     9 |                        2 |
Query OK, 6 row(s) in set (0.002613s)
```

1588
## Time Functions
D
dingbo 已提交
1589

1590
From version 2.6.0.0, below time related functions can be used in TDengine.
D
dingbo 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599

### NOW

```sql
SELECT NOW() FROM { tb_name | stb_name } [WHERE clause];
SELECT select_expr FROM { tb_name | stb_name } WHERE ts_col cond_operatior NOW();
INSERT INTO tb_name VALUES (NOW(), ...);
```

1600
**Description**: The current time of the client side system
D
dingbo 已提交
1601

1602
**Return value type**: TIMESTAMP
D
dingbo 已提交
1603

1604
**Applicable column types**: TIMESTAMP only
D
dingbo 已提交
1605

1606
**Applicable table types**: table, stable
D
dingbo 已提交
1607

1608
**More explanations**:
D
dingbo 已提交
1609

1610 1611 1612
- Add and Subtract operation can be performed, for example NOW() + 1s, the time unit can be:
  b(nanosecond), u(microsecond), a(millisecond)), s(second), m(minute), h(hour), d(day), w(week)
- The precision of the returned timestamp is same as the precision set for the current data base in use
D
dingbo 已提交
1613

1614
**Examples**:
D
dingbo 已提交
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646

```sql
taos> SELECT NOW() FROM meters;
          now()          |
==========================
  2022-02-02 02:02:02.456 |
Query OK, 1 row(s) in set (0.002093s)

taos> SELECT NOW() + 1h FROM meters;
       now() + 1h         |
==========================
  2022-02-02 03:02:02.456 |
Query OK, 1 row(s) in set (0.002093s)

taos> SELECT COUNT(voltage) FROM d1001 WHERE ts < NOW();
        count(voltage)       |
=============================
                           5 |
Query OK, 5 row(s) in set (0.004475s)

taos> INSERT INTO d1001 VALUES (NOW(), 10.2, 219, 0.32);
Query OK, 1 of 1 row(s) in database (0.002210s)
```

### TODAY

```sql
SELECT TODAY() FROM { tb_name | stb_name } [WHERE clause];
SELECT select_expr FROM { tb_name | stb_name } WHERE ts_col cond_operatior TODAY()];
INSERT INTO tb_name VALUES (TODAY(), ...);
```

1647
**Description**: The timestamp of 00:00:00 of the client side system
D
dingbo 已提交
1648

1649
**Return value type**: TIMESTAMP
D
dingbo 已提交
1650

1651
**Applicable column types**: TIMESTAMP only
D
dingbo 已提交
1652

1653
**Applicable table types**: table, stable
D
dingbo 已提交
1654

1655
**More explanations**:
D
dingbo 已提交
1656

1657 1658 1659
- Add and Subtract operation can be performed, for example NOW() + 1s, the time unit can be:
  b(nanosecond), u(microsecond), a(millisecond)), s(second), m(minute), h(hour), d(day), w(week)
- The precision of the returned timestamp is same as the precision set for the current data base in use
D
dingbo 已提交
1660

1661
**Examples**:
D
dingbo 已提交
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691

```sql
taos> SELECT TODAY() FROM meters;
         today()          |
==========================
  2022-02-02 00:00:00.000 |
Query OK, 1 row(s) in set (0.002093s)

taos> SELECT TODAY() + 1h FROM meters;
      today() + 1h        |
==========================
  2022-02-02 01:00:00.000 |
Query OK, 1 row(s) in set (0.002093s)

taos> SELECT COUNT(voltage) FROM d1001 WHERE ts < TODAY();
        count(voltage)       |
=============================
                           5 |
Query OK, 5 row(s) in set (0.004475s)

taos> INSERT INTO d1001 VALUES (TODAY(), 10.2, 219, 0.32);
Query OK, 1 of 1 row(s) in database (0.002210s)
```

### TIMEZONE

```sql
SELECT TIMEZONE() FROM { tb_name | stb_name } [WHERE clause];
```

1692
**Description**: The timezone of the client side system
D
dingbo 已提交
1693

1694
**Return value type**: BINARY
D
dingbo 已提交
1695

1696
**Applicable column types**: None
D
dingbo 已提交
1697

1698
**Applicable table types**: table, stable
D
dingbo 已提交
1699

1700
**Examples**:
D
dingbo 已提交
1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715

```sql
taos> SELECT TIMEZONE() FROM meters;
           timezone()           |
=================================
 UTC (UTC, +0000)               |
Query OK, 1 row(s) in set (0.002093s)
```

### TO_ISO8601

```sql
SELECT TO_ISO8601(ts_val | ts_col) FROM { tb_name | stb_name } [WHERE clause];
```

1716
**Description**: The ISO8601 date/time format converted from a UNIX timestamp, plus the timezone of the client side system
D
dingbo 已提交
1717

1718
**Return value type**: BINARY
D
dingbo 已提交
1719

1720
**Applicable column types**: TIMESTAMP, constant or a column
D
dingbo 已提交
1721

1722
**Applicable table types**: table, stable
D
dingbo 已提交
1723

1724
**More explanations**:
D
dingbo 已提交
1725

1726 1727
- If the input is UNIX timestamp constant, the precision of the returned value is determined by the digits of the input timestamp
- If the input is a column of TIMESTAMP type, The precision of the returned value is same as the precision set for the current data base in use
D
dingbo 已提交
1728

1729
**Examples**:
D
dingbo 已提交
1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

```sql
taos> SELECT TO_ISO8601(1643738400) FROM meters;
   to_iso8601(1643738400)    |
==============================
 2022-02-02T02:00:00+0800    |

taos> SELECT TO_ISO8601(ts) FROM meters;
       to_iso8601(ts)        |
==============================
 2022-02-02T02:00:00+0800    |
 2022-02-02T02:00:00+0800    |
 2022-02-02T02:00:00+0800    |
```

### TO_UNIXTIMESTAMP

```sql
SELECT TO_UNIXTIMESTAMP(datetime_string | ts_col) FROM { tb_name | stb_name } [WHERE clause];
```

1751
**Description**: UNIX timestamp converted from a string of date/time format
D
dingbo 已提交
1752

1753
**Return value type**: Long integer
D
dingbo 已提交
1754

1755
**Applicable column types**: Constant or column of BINARY/NCHAR
D
dingbo 已提交
1756

1757
**Applicable table types**: table, stable
D
dingbo 已提交
1758

1759
**More explanations**:
D
dingbo 已提交
1760

1761 1762
- The input string must be compatible with ISO8601/RFC3339 standard, 0 will be returned if the string can't be covnerted
- The precision of the returned timestamp is same as the precision set for the current data base in use
D
dingbo 已提交
1763

1764
**Examples**:
D
dingbo 已提交
1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785

```sql
taos> SELECT TO_UNIXTIMESTAMP("2022-02-02T02:00:00.000Z") FROM meters;
to_unixtimestamp("2022-02-02T02:00:00.000Z") |
==============================================
                               1643767200000 |

taos> SELECT TO_UNIXTIMESTAMP(col_binary) FROM meters;
      to_unixtimestamp(col_binary)     |
========================================
                         1643767200000 |
                         1643767200000 |
                         1643767200000 |
```

### TIMETRUNCATE

```sql
SELECT TIMETRUNCATE(ts_val | datetime_string | ts_col, time_unit) FROM { tb_name | stb_name } [WHERE clause];
```

1786 1787 1788
**Description**: Truncate the input timestamp with unit specified by `time_unit`\

**Return value type**: TIMESTAMP\
D
dingbo 已提交
1789

1790
**Applicable column types**: UNIX timestamp constant, string constant of date/time format, or a column of timestamp
D
dingbo 已提交
1791

1792
**Applicable table types**: table, stable
D
dingbo 已提交
1793

1794
**More explanations**:
D
dingbo 已提交
1795

1796 1797 1798
- Time unit specified by `time_unit` can be:
  1u(microsecond),1a(millisecond),1s(second),1m(minute),1h(hour),1d(day).
- The precision of the returned timestamp is same as the precision set for the current data base in use
D
dingbo 已提交
1799

1800
**Examples**:
D
dingbo 已提交
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829

```sql
taos> SELECT TIMETRUNCATE(1643738522000, 1h) FROM meters;
  timetruncate(1643738522000, 1h) |
===================================
      2022-02-02 02:00:00.000     |
Query OK, 1 row(s) in set (0.001499s)

taos> SELECT TIMETRUNCATE("2022-02-02 02:02:02", 1h) FROM meters;
  timetruncate("2022-02-02 02:02:02", 1h) |
===========================================
      2022-02-02 02:00:00.000             |
Query OK, 1 row(s) in set (0.003903s)

taos> SELECT TIMETRUNCATE(ts, 1h) FROM meters;
  timetruncate(ts, 1h)   |
==========================
 2022-02-02 02:00:00.000 |
 2022-02-02 02:00:00.000 |
 2022-02-02 02:00:00.000 |
Query OK, 3 row(s) in set (0.003903s)
```

### TIMEDIFF

```sql
SELECT TIMEDIFF(ts_val1 | datetime_string1 | ts_col1, ts_val2 | datetime_string2 | ts_col2 [, time_unit]) FROM { tb_name | stb_name } [WHERE clause];
```

1830 1831 1832
**Description**: The difference between two timestamps, and rounded to the time unit specified by `time_unit`

**Return value type**: Long Integer
D
dingbo 已提交
1833

1834
**Applicable column types**: UNIX timestamp constant, string constant of date/time format, or a column of TIMESTAMP type
D
dingbo 已提交
1835

1836
**Applicable table types**: table, stable
D
dingbo 已提交
1837

1838
**More explanations**:
D
dingbo 已提交
1839

1840 1841 1842
- Time unit specified by `time_unit` can be:
  1u(microsecond),1a(millisecond),1s(second),1m(minute),1h(hour),1d(day).
- The precision of the returned timestamp is same as the precision set for the current data base in use
D
dingbo 已提交
1843

1844
**Examples**:
D
dingbo 已提交
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869

```sql
taos> SELECT TIMEDIFF(1643738400000, 1643742000000) FROM meters;
 timediff(1643738400000, 1643742000000) |
=========================================
                                3600000 |
Query OK, 1 row(s) in set (0.002553s)
taos> SELECT TIMEDIFF(1643738400000, 1643742000000, 1h) FROM meters;
 timediff(1643738400000, 1643742000000, 1h) |
=============================================
                                          1 |
Query OK, 1 row(s) in set (0.003726s)

taos> SELECT TIMEDIFF("2022-02-02 03:00:00", "2022-02-02 02:00:00", 1h) FROM meters;
 timediff("2022-02-02 03:00:00", "2022-02-02 02:00:00", 1h) |
=============================================================
                                                          1 |
Query OK, 1 row(s) in set (0.001937s)

taos> SELECT TIMEDIFF(ts_col1, ts_col2, 1h) FROM meters;
   timediff(ts_col1, ts_col2, 1h) |
===================================
                                1 |
Query OK, 1 row(s) in set (0.001937s)
```