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

5
## Aggregate Functions
D
dingbo 已提交
6

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

### COUNT

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

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

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

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

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

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

25 26
- 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 已提交
27

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

```
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];
```

G
gccgdb1234 已提交
50
**Description**:Get the average value of a column in a table or STable
D
dingbo 已提交
51

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

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

G
gccgdb1234 已提交
56
**Applicable table types**:table, STable
D
dingbo 已提交
57

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

```
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;
```

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

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

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

G
gccgdb1234 已提交
86
**Applicable table types**:table, STable
D
dingbo 已提交
87

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

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

### IRATE

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

98
**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 已提交
99

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

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

G
gccgdb1234 已提交
104
**Applicable table types**:table, STable
D
dingbo 已提交
105

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

G
gccgdb1234 已提交
108
- 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 已提交
109 110 111 112 113 114 115

### SUM

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

G
gccgdb1234 已提交
116
**Description**:The sum of a specific column in a table or STable
D
dingbo 已提交
117

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

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

G
gccgdb1234 已提交
122
**Applicable table types**:table, STable
D
dingbo 已提交
123

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

```
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];
```

G
gccgdb1234 已提交
146
**Description**:Standard deviation of a specific column in a table or STable
D
dingbo 已提交
147

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

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

G
gccgdb1234 已提交
152
**Applicable table types**:table, STable (starting from version 2.0.15.1)
D
dingbo 已提交
153

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

```
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];
```

170
**Description**: The linear regression function of the specified column and the timestamp column (primary key), `start_val` is the initial value and `step_val` is the step value.
D
dingbo 已提交
171

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

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

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

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

```
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];
```

194
**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 已提交
195

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

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

200
**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 已提交
201

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

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

```
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];
```

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

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

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

235
**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 已提交
236

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

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

```
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)
```

262
## Selection Functions
D
dingbo 已提交
263

264
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 已提交
265 266 267 268 269 270 271

### MIN

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

G
gccgdb1234 已提交
272
**Description**:The minimum value of a specific column in a table or STable
D
dingbo 已提交
273

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

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

G
gccgdb1234 已提交
278
**Applicable table types**:table, STable
D
dingbo 已提交
279

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

```
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];
```

G
gccgdb1234 已提交
302
**Description**:The maximum value of a specific column of a table or STable
D
dingbo 已提交
303

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

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

G
gccgdb1234 已提交
308
**Applicable table types**:table, STable
D
dingbo 已提交
309

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

```
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];
```

G
gccgdb1234 已提交
332
**Description**:The first non-null value of a specific column in a table or STable
D
dingbo 已提交
333

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

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

G
gccgdb1234 已提交
338
**Applicable table types**:table, STable
D
dingbo 已提交
339

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

342 343 344
- 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 已提交
345

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

```
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];
```

G
gccgdb1234 已提交
368
**Description**:The last non-NULL value of a specific column in a table or STable
D
dingbo 已提交
369

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

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

G
gccgdb1234 已提交
374
**Applicable table types**:table, STable
D
dingbo 已提交
375

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

378 379
- 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.
G
gccgdb1234 已提交
380
- 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 已提交
381

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

```
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];
```

G
gccgdb1234 已提交
404
**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 已提交
405

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

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

G
gccgdb1234 已提交
410
**Applicable table types**:table, STable
D
dingbo 已提交
411

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

414 415 416
- _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 已提交
417

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

```
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];
```

G
gccgdb1234 已提交
443
**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 已提交
444

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

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

G
gccgdb1234 已提交
449
**Applicable table types**: table, STable
D
dingbo 已提交
450

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

453 454 455
- _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 已提交
456

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

```
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];
```

481
**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 已提交
482

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

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

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

489
**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 已提交
490

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

```
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]
```

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

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

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

G
gccgdb1234 已提交
514
**Applicable table types**: table, STable
D
dingbo 已提交
515

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

518 519 520
- _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 已提交
521

522
**Nested query**: It can be used in both the outer query and inner query in a nested query.
D
dingbo 已提交
523 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

```
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 };
```

G
gccgdb1234 已提交
550
**Description**: The last row of a table or STable
D
dingbo 已提交
551

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

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

G
gccgdb1234 已提交
556
**Applicable table types**: table, STable
D
dingbo 已提交
557

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

G
gccgdb1234 已提交
560
- 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.
561
- Can't be used with `INTERVAL`.
D
dingbo 已提交
562

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

```
 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)
```

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

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

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

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

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

G
gccgdb1234 已提交
591
**Applicable table types**: table, STable, nested query
D
dingbo 已提交
592

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

595 596 597 598 599
- `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.
G
gccgdb1234 已提交
600
- `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.
601
- The result of `INTERP` is not influenced by `ORDER BY TIMESTAMP`, which impacts the output order only..
D
dingbo 已提交
602

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

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

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

611
- 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 已提交
612 613 614 615 616

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

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

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

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

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

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

```
632
  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 已提交
633 634
```

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

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

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

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

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

G
gccgdb1234 已提交
647
**Applicable table types**: table, STable
D
dingbo 已提交
648

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

651 652
- 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.
G
gccgdb1234 已提交
653
- 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.
654 655
- `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 已提交
656 657 658 659 660 661 662 663 664

```
 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)
```

665
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 已提交
666 667 668 669 670 671 672 673 674 675 676 677

```
 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)
```

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

```
 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];
```

695
**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 已提交
696

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

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

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

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

705
**Examples**:
D
dingbo 已提交
706 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

```
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];
```

733
**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 已提交
734

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

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

739
**Applicable versions**: From version 2.6.0.0
D
dingbo 已提交
740

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

G
gccgdb1234 已提交
743
- It can be used against table or STable, but can't be used together with time window, like `interval`, `state_window` or `session_window` .
744
- 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 已提交
745

746
**Examples**:
D
dingbo 已提交
747 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

```
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)
```

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

### DIFF

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

781
**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 已提交
782

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

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

G
gccgdb1234 已提交
787
**Applicable table types**: table, STable
D
dingbo 已提交
788

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

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

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

797 798 799 800 801 802 803 804
```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 已提交
805 806 807 808 809 810 811

### DERIVATIVE

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

812
**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 已提交
813

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

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

G
gccgdb1234 已提交
818
**Applicable table types**: table, STable
D
dingbo 已提交
819

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

822
- 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.\
G
gccgdb1234 已提交
823
- It can be used together with `GROUP BY tbname` against a STable.
D
dingbo 已提交
824

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

```
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];
```

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

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

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

G
gccgdb1234 已提交
851
**Applicable table types**: table, STable
D
dingbo 已提交
852

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

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

```
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];
```

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

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

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

G
gccgdb1234 已提交
883
**Applicable table types**: table, STable
D
dingbo 已提交
884

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

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

889 890 891
- 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 已提交
892 893 894 895 896 897 898

### FLOOR

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

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

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

### ROUND

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

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

**More explanations**: The restrictions are same as `CEIL` function.
D
dingbo 已提交
912 913 914

### CSUM

G
gccgdb1234 已提交
915
```sql
D
dingbo 已提交
916
    SELECT CSUM(field_name) FROM { tb_name | stb_name } [WHERE clause]
G
gccgdb1234 已提交
917
```
D
dingbo 已提交
918

919
**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 已提交
920

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

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

G
gccgdb1234 已提交
925
**Applicable table types**: table, STable
D
dingbo 已提交
926

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

929 930
**More explanations**:

G
gccgdb1234 已提交
931
- Can't be used on tags when it's used on STable
932 933
- Arithmetic operation can't be performed on the result of `csum` function
- Can only be used with aggregate functions
G
gccgdb1234 已提交
934
- `Group by tbname` must be used together on a STable to force the result on a single timeline
935 936

**Applicable versions**: From 2.3.0.x
D
dingbo 已提交
937 938 939

### MAVG

G
gccgdb1234 已提交
940
```sql
D
dingbo 已提交
941
    SELECT MAVG(field_name, K) FROM { tb_name | stb_name } [WHERE clause]
G
gccgdb1234 已提交
942
```
D
dingbo 已提交
943

944 945 946
**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 已提交
947

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

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

G
gccgdb1234 已提交
952
**Applicable table types**: table, STable
D
dingbo 已提交
953

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

956 957
- Arithmetic operation can't be performed on the result of `MAVG`.
- Can only be used with data columns, can't be used with tags.
958 959
- Can't be used with aggregate functions.
- Must be used with `GROUP BY tbname` when it's used on a STable to force the result on each single timeline.
960 961

**Applicable versions**: From 2.3.0.x
D
dingbo 已提交
962 963 964

### SAMPLE

G
gccgdb1234 已提交
965
```sql
D
dingbo 已提交
966
    SELECT SAMPLE(field_name, K) FROM { tb_name | stb_name } [WHERE clause]
G
gccgdb1234 已提交
967
```
D
dingbo 已提交
968

969 970 971
**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 已提交
972

G
gccgdb1234 已提交
973
**Applicable data types**: Any data type except for tags of STable
D
dingbo 已提交
974

G
gccgdb1234 已提交
975
**Applicable table types**: table, STable
D
dingbo 已提交
976

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

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

981
- Arithmetic operation can't be operated on the result of `SAMPLE` function
G
gccgdb1234 已提交
982
- Must be used with `Group by tbname` when it's used on a STable to force the result on each single timeline
983 984

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

### ASIN

988 989 990 991 992 993 994
```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 已提交
995

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

G
gccgdb1234 已提交
998
**Applicable table types**: table, STable
D
dingbo 已提交
999

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

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

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

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

### ACOS

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

1015
**Description**: The anti-cosine of a specific column
1016

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

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

G
gccgdb1234 已提交
1021
**Applicable table types**: table, STable
D
dingbo 已提交
1022

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

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

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

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

### ATAN

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

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

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

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

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

G
gccgdb1234 已提交
1046
**Applicable table types**: table, STable
D
dingbo 已提交
1047

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

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

**More explanations**:

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

### SIN

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

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

1065
**Description**: The anti-cosine of a specific column
1066 1067

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

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

G
gccgdb1234 已提交
1071
**Applicable table types**: table, STable
D
dingbo 已提交
1072

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

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

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

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

### COS

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

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

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

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

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

G
gccgdb1234 已提交
1096
**Applicable table types**: table, STable
D
dingbo 已提交
1097

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

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

1102 1103 1104 1105
**More explanations**:

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

### TAN

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

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

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

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

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

G
gccgdb1234 已提交
1121
**Applicable table types**: table, STable
D
dingbo 已提交
1122

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

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

1127 1128 1129 1130
**More explanations**:

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

### POW

1134 1135 1136 1137 1138 1139 1140
```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 已提交
1141

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

G
gccgdb1234 已提交
1144
**Applicable table types**: table, STable
D
dingbo 已提交
1145

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

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

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

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

### LOG

1157 1158 1159 1160 1161 1162 1163
```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 已提交
1164

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

G
gccgdb1234 已提交
1167
**Applicable table types**: table, STable
D
dingbo 已提交
1168

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

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

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

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

### ABS

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

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

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

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

G
gccgdb1234 已提交
1190
**Applicable table types**: table, STable
D
dingbo 已提交
1191

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

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

1196 1197 1198 1199
**More explanations**:

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

### SQRT

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

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

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

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

G
gccgdb1234 已提交
1213
**Applicable table types**: table, STable
D
dingbo 已提交
1214

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

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

1219 1220 1221 1222
**More explanations**:

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

### CAST

1226 1227 1228 1229 1230
```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 已提交
1231

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

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

1236 1237
- 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 已提交
1238

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

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

1243 1244 1245 1246 1247 1248 1249
- 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 已提交
1250 1251 1252

### CONCAT

1253 1254 1255 1256 1257
```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 已提交
1258

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

1261
**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 已提交
1262

G
gccgdb1234 已提交
1263
**Applicable table types**: table, STable
D
dingbo 已提交
1264

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

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

### CONCAT_WS

1271 1272 1273 1274 1275
```
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 已提交
1276

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

1279
**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 已提交
1280

G
gccgdb1234 已提交
1281
**Applicable table types**: table, STable
D
dingbo 已提交
1282

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

1285 1286 1287 1288 1289
**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 已提交
1290 1291 1292

### LENGTH

1293 1294 1295 1296 1297 1298 1299 1300 1301
```
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 已提交
1302

G
gccgdb1234 已提交
1303
**Applicable table types**: table, STable
D
dingbo 已提交
1304

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

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

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

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

### CHAR_LENGTH

1315 1316 1317 1318 1319 1320 1321
```
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 已提交
1322

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

G
gccgdb1234 已提交
1325
**Applicable table types**: table, STable
D
dingbo 已提交
1326

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

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

1331 1332 1333
**More explanations**

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

### LOWER

1337 1338 1339 1340 1341 1342 1343
```
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 已提交
1344

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

G
gccgdb1234 已提交
1347
**Applicable table types**: table, STable
D
dingbo 已提交
1348

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

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

1353 1354 1355
**More explanations**

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

### UPPER

1359 1360 1361 1362 1363 1364 1365
```
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 已提交
1366

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

G
gccgdb1234 已提交
1369
**Applicable table types**: table, STable
D
dingbo 已提交
1370

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

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

1375 1376 1377
**More explanations**

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

### LTRIM

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

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

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

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

G
gccgdb1234 已提交
1391
**Applicable table types**: table, STable
D
dingbo 已提交
1392

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

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

**More explanations**

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

### RTRIM

1403 1404 1405 1406 1407 1408 1409 1410 1411
```
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 已提交
1412

G
gccgdb1234 已提交
1413
**Applicable table types**: table, STable
D
dingbo 已提交
1414

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

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

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

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

### SUBSTR

1425 1426 1427 1428 1429
```
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 已提交
1430

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

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

G
gccgdb1234 已提交
1435
**Applicable table types**: table, STable
D
dingbo 已提交
1436

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

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

1441 1442 1443 1444 1445 1446 1447
**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 已提交
1448 1449 1450 1451 1452

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

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

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

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

G
gccgdb1234 已提交
1459
**Applicable table types**: table, STable
D
dingbo 已提交
1460

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

1463 1464 1465 1466
- 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 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483

```
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];
```

1484 1485 1486
**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 已提交
1487

1488 1489
- 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 已提交
1490

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

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

G
gccgdb1234 已提交
1495
**Applicable table types**: table, STable
D
dingbo 已提交
1496

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

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

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

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

1506
**Examples**:
D
dingbo 已提交
1507 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

```
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];
```

1538
**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 已提交
1539

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

1542 1543 1544
- 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 已提交
1545

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

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

G
gccgdb1234 已提交
1550
**Applicable table types**: table, STable
D
dingbo 已提交
1551

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

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

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

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

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

```
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)
```

1587
## Time Functions
D
dingbo 已提交
1588

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

### 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(), ...);
```

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

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

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

G
gccgdb1234 已提交
1605
**Applicable table types**: table, STable
D
dingbo 已提交
1606

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

1609 1610 1611
- 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 已提交
1612

1613
**Examples**:
D
dingbo 已提交
1614 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

```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(), ...);
```

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

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

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

G
gccgdb1234 已提交
1652
**Applicable table types**: table, STable
D
dingbo 已提交
1653

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

1656 1657 1658
- 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 已提交
1659

1660
**Examples**:
D
dingbo 已提交
1661 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

```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];
```

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

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

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

G
gccgdb1234 已提交
1697
**Applicable table types**: table, STable
D
dingbo 已提交
1698

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

```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];
```

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

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

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

G
gccgdb1234 已提交
1721
**Applicable table types**: table, STable
D
dingbo 已提交
1722

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

1725 1726
- 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 已提交
1727

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

```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];
```

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

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

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

G
gccgdb1234 已提交
1756
**Applicable table types**: table, STable
D
dingbo 已提交
1757

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

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

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

```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];
```

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

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

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

G
gccgdb1234 已提交
1791
**Applicable table types**: table, STable
D
dingbo 已提交
1792

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

1795 1796 1797
- 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 已提交
1798

1799
**Examples**:
D
dingbo 已提交
1800 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

```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];
```

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

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

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

G
gccgdb1234 已提交
1835
**Applicable table types**: table, STable
D
dingbo 已提交
1836

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

1839 1840 1841
- 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 已提交
1842

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

```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)
```