07-function.md 40.4 KB
Newer Older
1
---
2
title: Functions
3
toc_max_heading_level: 4
4 5
---

6
## Single-Row Functions
7

8
Single-Row functions return a result row for each row in the query result.
9

10
### Numeric Functions
11

12
#### ABS
13

14 15 16
```sql
SELECT ABS(field_name) FROM { tb_name | stb_name } [WHERE clause]
```
17

18
**Description**: The absolute of a specific column.
19

20
**Return value type**: UBIGINT if the input value is integer; DOUBLE if the input value is FLOAT/DOUBLE.
21

22
**Applicable data types**: Numeric types.
23

24
**Applicable table types**: table, STable.
25

26
**Applicable nested query**: Inner query and Outer query.
27

28 29
**More explanations**:
- Can't be used with aggregate functions.
30

31
#### ACOS
32

33 34
```sql
SELECT ACOS(field_name) FROM { tb_name | stb_name } [WHERE clause]
35 36
```

37
**Description**: The anti-cosine of a specific column
38

39
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
40

41
**Applicable data types**: Numeric types.
42

S
Sean Ely 已提交
43
**Applicable table types**: table, STable
44

45
**Applicable nested query**: Inner query and Outer query
46

47 48
**More explanations**:
- Can't be used with aggregate functions
49

50
#### ASIN
51

52 53
```sql
SELECT ASIN(field_name) FROM { tb_name | stb_name } [WHERE clause]
54 55
```

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

58
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
59

60
**Applicable data types**: Numeric types.
61

S
Sean Ely 已提交
62
**Applicable table types**: table, STable
63

64 65
**Applicable nested query**: Inner query and Outer query

66
**More explanations**:
67
- Can't be used with aggregate functions
68

69
#### ATAN
70

71 72
```sql
SELECT ATAN(field_name) FROM { tb_name | stb_name } [WHERE clause]
73 74
```

75
**Description**: anti-tangent of a specific column
76

77
**Description**: The anti-cosine of a specific column
78

79 80 81
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL

**Applicable data types**: Numeric types.
82

S
Sean Ely 已提交
83
**Applicable table types**: table, STable
84

85 86
**Applicable nested query**: Inner query and Outer query

87
**More explanations**:
88
- Can't be used with aggregate functions
89

90
#### CEIL
91 92

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

96
**Description**: The rounded up value of a specific column
97

98
**Return value type**: Same as the column being used
99

100
**Applicable data types**: Numeric types.
101

S
Sean Ely 已提交
102
**Applicable table types**: table, STable
103

104
**Applicable nested query**: Inner query and outer query
105

106 107 108
**More explanations**:
- Arithmetic operation can be performed on the result of `ceil` function
- Can't be used with aggregate functions
109

110
#### COS
111

112 113
```sql
SELECT COS(field_name) FROM { tb_name | stb_name } [WHERE clause]
114 115
```

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

118
**Description**: The anti-cosine of a specific column
119

120 121 122 123 124 125 126 127 128 129
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL

**Applicable data types**: Numeric types.

**Applicable table types**: table, STable

**Applicable nested query**: Inner query and Outer query

**More explanations**:
- Can't be used with aggregate functions
130

131
#### FLOOR
132 133

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

137
**Description**: The rounded down value of a specific column
138

139 140 141 142 143 144
**More explanations**: The restrictions are same as those of the `CEIL` function.

#### LOG

```sql
SELECT LOG(field_name, base) FROM { tb_name | stb_name } [WHERE clause]
145 146
```

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

149
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
150

151
**Applicable data types**: Numeric types.
152

153
**Applicable table types**: table, STable
154

155
**Applicable nested query**: Inner query and Outer query
156

157 158
**More explanations**:
- Can't be used with aggregate functions
159

160
#### POW
161

162 163 164
```sql
SELECT POW(field_name, power) FROM { tb_name | stb_name } [WHERE clause]
```
165

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

168
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
169

170
**Applicable data types**: Numeric types.
171

172
**Applicable table types**: table, STable
173

174
**Applicable nested query**: Inner query and Outer query
175

176 177
**More explanations**:
- Can't be used with aggregate functions
178

179
#### ROUND
180 181

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

185
**Description**: The rounded value of a specific column.
186

187
**More explanations**: The restrictions are same as `CEIL` function.
188

189
#### SIN
190

191 192 193
```sql
SELECT SIN(field_name) FROM { tb_name | stb_name } [WHERE clause]
```
194

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

197
**Description**: The anti-cosine of a specific column
198

199
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
200

201
**Applicable data types**: Numeric types.
202

203
**Applicable table types**: table, STable
204

205
**Applicable nested query**: Inner query and Outer query
206

207 208
**More explanations**:
- Can't be used with aggregate functions
209

210
#### SQRT
211

212 213 214
```sql
SELECT SQRT(field_name) FROM { tb_name | stb_name } [WHERE clause]
```
215

216
**Description**: The square root of a specific column
217

218
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
219

220
**Applicable data types**: Numeric types.
G
gccgdb1234 已提交
221

222
**Applicable table types**: table, STable
G
gccgdb1234 已提交
223

224
**Applicable nested query**: Inner query and Outer query
225

226 227
**More explanations**:
- Can't be used with aggregate functions
G
gccgdb1234 已提交
228

229
#### TAN
230

231 232
```sql
SELECT TAN(field_name) FROM { tb_name | stb_name } [WHERE clause]
233 234
```

235
**Description**: The tangent of a specific column
G
gccgdb1234 已提交
236

237
**Description**: The anti-cosine of a specific column
G
gccgdb1234 已提交
238

239
**Return value type**: Double if the input value is not NULL; or NULL if the input value is NULL
G
gccgdb1234 已提交
240

241
**Applicable data types**: Numeric types.
G
gccgdb1234 已提交
242

243
**Applicable table types**: table, STable
G
gccgdb1234 已提交
244

245
**Applicable nested query**: Inner query and Outer query
G
gccgdb1234 已提交
246

247 248
**More explanations**:
- Can't be used with aggregate functions
G
gccgdb1234 已提交
249

250
### String Functions
251

252
String functiosn take strings as input and output numbers or strings.
253

254
#### CHAR_LENGTH
255 256

```
257
SELECT CHAR_LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
258 259
```

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

262
**Return value type**: Integer
263

G
gccgdb1234 已提交
264
**Applicable data types**: VARCHAR or NCHAR
265

S
Sean Ely 已提交
266
**Applicable table types**: table, STable
267

268
**Applicable nested query**: Inner query and Outer query
269

270
**More explanations**
271

272
- If the input value is NULL, the output is NULL too
273

274
#### CONCAT
275

276 277
```sql
SELECT CONCAT(str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
278 279
```

280
**Description**: The concatenation result of two or more strings, the number of strings to be concatenated is at least 2 and at most 8
281

G
gccgdb1234 已提交
282
**Return value type**: If all input strings are VARCHAR type, the result is VARCHAR type too. If any one of input strings is NCHAR type, then the result is NCHAR.
283

G
gccgdb1234 已提交
284
**Applicable data types**: VARCHAR, NCHAR.  At least 2 input strings are requird, and at most 8 input strings are allowed. 
285

S
Sean Ely 已提交
286
**Applicable table types**: table, STable
287

288
**Applicable nested query**: Inner query and Outer query
289

290
#### CONCAT_WS
291 292

```
293
SELECT CONCAT_WS(separator, str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
294 295
```

296
**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
297

G
gccgdb1234 已提交
298
**Return value type**: If all input strings are VARCHAR type, the result is VARCHAR type too. If any one of input strings is NCHAR type, then the result is NCHAR.
299

G
gccgdb1234 已提交
300
**Applicable data types**: VARCHAR, NCHAR. At least 3 input strings are requird, and at most 9 input strings are allowed. 
301

S
Sean Ely 已提交
302
**Applicable table types**: table, STable
303

304 305
**Applicable nested query**: Inner query and Outer query

306 307
**More explanations**:

308
- 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.
309

310
#### LENGTH
311 312

```
313
SELECT LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
314 315
```

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

318
**Return value type**: Integer
319

G
gccgdb1234 已提交
320
**Applicable data types**: VARCHAR or NCHAR
S
Sean Ely 已提交
321
**Applicable table types**: table, STable
322

323
**Applicable nested query**: Inner query and Outer query
324

325
**More explanations**
326

327
- If the input value is NULL, the output is NULL too
328

329
#### LOWER
330 331

```
332
SELECT LOWER(str|column) FROM { tb_name | stb_name } [WHERE clause]
333 334
```

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

337
**Return value type**: Same as input
338

G
gccgdb1234 已提交
339
**Applicable data types**: VARCHAR or NCHAR
340

S
Sean Ely 已提交
341
**Applicable table types**: table, STable
342

343
**Applicable nested query**: Inner query and Outer query
344

345
**More explanations**
346

347
- If the input value is NULL, the output is NULL too
348

349
#### LTRIM
350 351

```
352
SELECT LTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
353 354
```

355
**Description**: Remove the left leading blanks of a string
356

357
**Return value type**: Same as input
358

G
gccgdb1234 已提交
359
**Applicable data types**: VARCHAR or NCHAR
360 361 362

**Applicable table types**: table, STable

363
**Applicable nested query**: Inner query and Outer query
364

365
**More explanations**
366

367
- If the input value is NULL, the output is NULL too
368

369
#### RTRIM
370 371

```
372
SELECT RTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
373 374
```

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

377
**Return value type**: Same as input
378

G
gccgdb1234 已提交
379
**Applicable data types**: VARCHAR or NCHAR
380

381
**Applicable table types**: table, STable
382

383
**Applicable nested query**: Inner query and Outer query
384

385
**More explanations**
386

387
- If the input value is NULL, the output is NULL too
388

389
#### SUBSTR
390 391

```
392
SELECT SUBSTR(str,pos[,len]) FROM { tb_name | stb_name } [WHERE clause]
393 394
```

395
**Description**: The sub-string starting from `pos` with length of `len` from the original string `str`
396

397
**Return value type**: Same as input
398

G
gccgdb1234 已提交
399
**Applicable data types**: VARCHAR or NCHAR
400 401 402

**Applicable table types**: table, STable

403
**Applicable nested query**: Inner query and Outer query
404

405
**More explanations**:
406

407 408 409
- 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.
410

411
#### UPPER
412 413

```
414
SELECT UPPER(str|column) FROM { tb_name | stb_name } [WHERE clause]
415 416
```

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

419
**Return value type**: Same as input
420

G
gccgdb1234 已提交
421
**Applicable data types**: VARCHAR or NCHAR
422 423 424

**Applicable table types**: table, STable

425
**Applicable nested query**: Inner query and Outer query
426

427
**More explanations**
428

429
- If the input value is NULL, the output is NULL too
430

431
### Conversion Functions
432

433
This kind of functions convert from one data type to another one.
434

435
#### CAST
436

437 438
```sql
SELECT CAST(expression AS type_name) FROM { tb_name | stb_name } [WHERE clause]
439 440
```

G
gccgdb1234 已提交
441
**Description**: It's used for type casting. The input parameter `expression` can be data columns, constants, scalar functions or arithmetic between them. 
442

443
**Return value type**: The type specified by parameter `type_name`
444

445
**Applicable data types**:
446

G
gccgdb1234 已提交
447 448
- Parameter `expression` can be any data type except for JSON
- The output data type specified by `type_name` can only be one of BIGINT/VARCHAR(N)/TIMESTAMP/NCHAR(N)/BIGINT UNSIGNED
449

450
**More explanations**:
451

452 453 454
- 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:
G
gccgdb1234 已提交
455
  1)When casting VARCHAR/NCHAR to BIGINT/BIGINT UNSIGNED, some characters may be treated as illegal, for example "a" may be converted to 0.
456 457 458
  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
459

460
#### TO_ISO8601
461

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

466
**Description**: The ISO8601 date/time format converted from a UNIX timestamp, plus the timezone of the client side system
467

G
gccgdb1234 已提交
468
**Return value type**: VARCHAR
469

470
**Applicable column types**: TIMESTAMP, constant or a column
471

472
**Applicable table types**: table, STable
473

474
**More explanations**:
475

476 477
- 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
478

479
#### TO_JSON
480

481 482
```sql
SELECT TO_JSON(str_literal) FROM { tb_name | stb_name } [WHERE clause];
483 484
```

485
**Description**: Convert a JSON string to a JSON body。
486

487
**Return value type**: JSON
488

489
**Applicable column types**: JSON string, in the format like '{ "literal" : literal }'. '{}' is NULL value. keys in the string must be string constants, values can be constants of numeric types, bool, string or NULL. Escaping characters are not allowed in the JSON string.
490 491 492

**Applicable table types**: table, STable

493
**Applicable nested query**: Inner query and Outer query.
494

495
#### TO_UNIXTIMESTAMP
496

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

501
**Description**: UNIX timestamp converted from a string of date/time format
502

503
**Return value type**: Long integer
504

G
gccgdb1234 已提交
505
**Applicable column types**: Constant or column of VARCHAR/NCHAR
506

507
**Applicable table types**: table, STable
508

509
**More explanations**:
510

511 512
- The input string must be compatible with ISO8601/RFC3339 standard, 0 will be returned if the string can't be converted
- The precision of the returned timestamp is same as the precision set for the current data base in use
513

514
### DateTime Functions
515

516
This kind of functiosn oeprate on timestamp data. NOW(), TODAY() and TIMEZONE() are executed only once even though they may occurr multiple times in a single SQL statement.
517

518
#### NOW
519

520 521 522 523 524
```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(), ...);
```
525

526
**Description**: The current time of the client side system
527

528
**Return value type**: TIMESTAMP
529

530
**Applicable column types**: TIMESTAMP only
531

532
**Applicable table types**: table, STable
533

534
**More explanations**:
535

536 537 538
- 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
539

540 541 542 543
#### 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];
544 545
```

546
**Description**: The difference between two timestamps, and rounded to the time unit specified by `time_unit`
547

548
**Return value type**: Long Integer
549

550
**Applicable column types**: UNIX timestamp constant, string constant of date/time format, or a column of TIMESTAMP type
551

552
**Applicable table types**: table, STable
553 554 555

**More explanations**:

556 557 558
- 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
559

560
#### TIMETRUNCATE
561 562

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

566
**Description**: Truncate the input timestamp with unit specified by `time_unit`
567

568
**Return value type**: TIMESTAMP
569

570
**Applicable column types**: UNIX timestamp constant, string constant of date/time format, or a column of timestamp
571 572 573 574 575

**Applicable table types**: table, STable

**More explanations**:

576 577 578
- 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
579

580
#### TIMEZONE
581 582

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

586
**Description**: The timezone of the client side system
587

G
gccgdb1234 已提交
588
**Return value type**: VARCHAR
589

590
**Applicable column types**: None
591 592 593

**Applicable table types**: table, STable

594
#### TODAY
595

596 597 598 599
```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(), ...);
600 601
```

602
**Description**: The timestamp of 00:00:00 of the client side system
603

604
**Return value type**: TIMESTAMP
605

606
**Applicable column types**: TIMESTAMP only
607 608 609

**Applicable table types**: table, STable

610
**More explanations**:
611

612 613 614
- 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
615

616
## Aggregate Functions
617

618
Aggregate functions return single result row for each group in the query result set. Groups are determined by `GROUP BY` clause or time window clause if they are used; or the whole result is considered a group if neither of them is used.
619

620
### AVG
621 622

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

626
**Description**: Get the average value of a column in a table or STable
627

628
**Return value type**: Double precision floating number
629

G
gccgdb1234 已提交
630
**Applicable column types**: Numeric type
631 632 633

**Applicable table types**: table, STable

634
### COUNT
635 636

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

640
**Description**: Get the number of rows or the number of non-null values in a table or a super table.
641

642
**Return value type**: Long integer INT64
643

644
**Applicable column types**: All
645

646
**Applicable table types**: table, super table, sub table
647

648
**More explanation**:
649

650 651
- Wildcard (\*) is used to represent all columns. The `COUNT` function is used to get the total number of all rows.
- The number of non-NULL values will be returned if this function is used on a specific column.
652

653
### ELAPSED
654

655 656
```mysql
SELECT ELAPSED(field_name[, time_unit]) FROM { tb_name | stb_name } [WHERE clause] [INTERVAL(interval [, offset]) [SLIDING sliding]];
657 658
```

659
**Description**`elapsed` function can be used to calculate the continuous time length in which there is valid data. If it's used with `INTERVAL` clause, the returned result is the calcualted time length within each time window. If it's used without `INTERVAL` caluse, the returned result is the calculated time length within the specified time range. Please be noted that the return value of `elapsed` is the number of `time_unit` in the calculated time length.
660

661
**Return value type**:Double
662

663
**Applicable Column type**:Timestamp
664

665
**Applicable tables**: table, STable, outter in nested query
666

667
**Explanations**
668

669 670 671 672 673 674 675 676
- `field_name` parameter can only be the first column of a table, i.e. timestamp primary key.
- The minimum value of `time_unit` is the time precision of the database. If `time_unit` is not specified, the time precision of the database is used as the default ime unit.
- It can be used with `INTERVAL` to get the time valid time length of each time window. Please be noted that the return value is same as the time window for all time windows except for the first and the last time window.
- `order by asc/desc` has no effect on the result.
- `group by tbname` must be used together when `elapsed` is used against a STable.
- `group by` must NOT be used together when `elapsed` is used against a table or sub table.
- When used in nested query, it's only applicable when the inner query outputs an implicit timestamp column as the primary key. For example, `select elapsed(ts) from (select diff(value) from sub1)` is legal usage while `select elapsed(ts) from (select * from sub1)` is not.
- It can't be used with `leastsquares`, `diff`, `derivative`, `top`, `bottom`, `last_row`, `interp`.
677

678
### LEASTSQUARES
679

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

684
**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.
685

686
**Return value type**: A string in the format of "(slope, intercept)"
687

G
gccgdb1234 已提交
688
**Applicable column types**: Numeric types
689

690
**Applicable table types**: table only
691

692
### MODE
693

694 695 696
```
SELECT MODE(field_name) FROM tb_name [WHERE clause];
```
697

G
gccgdb1234 已提交
698
**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.
699

700
**Return value type**:Same as the data type of the column being operated upon
701

702
**Applicable column types**:Data types except for timestamp
703

704
**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.
705

706
### SPREAD
707

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

712
**Description**: The difference between the max and the min of a specific column
713

714
**Return value type**: Double precision floating point
715

G
gccgdb1234 已提交
716
**Applicable column types**: Numeric types
717 718 719

**Applicable table types**: table, STable

720
**More explanations**: Can be used on a column of TIMESTAMP type, the result is the time range size.
721

722
### STDDEV
723

724 725
```
SELECT STDDEV(field_name) FROM tb_name [WHERE clause];
726 727
```

728
**Description**: Standard deviation of a specific column in a table or STable
729

730
**Return value type**: Double precision floating number
731

G
gccgdb1234 已提交
732
**Applicable column types**: Numeric types
733 734 735

**Applicable table types**: table, STable

736
### SUM
737

738 739 740
```
SELECT SUM(field_name) FROM tb_name [WHERE clause];
```
741

742
**Description**: The sum of a specific column in a table or STable
743

744
**Return value type**: Double precision floating number or long integer
745

G
gccgdb1234 已提交
746
**Applicable column types**: Numeric types
747

748
**Applicable table types**: table, STable
749

750
### HYPERLOGLOG
751

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

756
**Description**:The cardinal number of a specific column is returned by using hyperloglog algorithm.
757

758
**Return value type**:Integer
759

760
**Applicable column types**:Any data type
761

762
**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.
763

764
### HISTOGRAM
765

766 767 768
```
SELECT HISTOGRAM(field_name,bin_type, bin_description, normalized) FROM tb_name [WHERE clause];
```
769

770
**Description**:Returns count of data points in user-specified ranges.
771

772
**Return value type**:Double or INT64, depends on normalized parameter settings.
773

774
**Applicable column type**:Numerical types.
775

776
**Applicable table types**: table, STable
777

778
**Explanations**
779

780 781
1. bin_type: parameter to indicate the bucket type, valid inputs are: "user_input", "linear_bin", "log_bin"。
2. bin_description: parameter to describe how to generate buckets,can be in the following JSON formats for each bin_type respectively:
782

783
   - "user_input": "[1, 3, 5, 7]": User specified bin values.
784

785 786 787 788 789 790
   - "linear_bin": "{"start": 0.0, "width": 5.0, "count": 5, "infinity": true}"
     "start" - bin starting point.
     "width" - bin offset.
     "count" - number of bins generated.
     "infinity" - whether to add(-inf, inf)as start/end point in generated set of bins.
     The above "linear_bin" descriptor generates a set of bins: [-inf, 0.0, 5.0, 10.0, 15.0, 20.0, +inf].
791

792 793 794 795 796 797
   - "log_bin": "{"start":1.0, "factor": 2.0, "count": 5, "infinity": true}"
     "start" - bin starting point.
     "factor" - exponential factor of bin offset.
     "count" - number of bins generated.
     "infinity" - whether to add(-inf, inf)as start/end point in generated range of bins.
     The above "log_bin" descriptor generates a set of bins:[-inf, 1.0, 2.0, 4.0, 8.0, 16.0, +inf].
798

799
3. normalized: setting to 1/0 to turn on/off result normalization.
800

801
## Selector Functions
802

803
Selector functiosn choose one or more rows in the query result set to retrun according toe the semantics. You can specify to output ts column and other columns including tbname and tags so that you can easily know which rows the selected values belong to.
804

805
### APERCENTILE
806

807 808 809 810
```
SELECT APERCENTILE(field_name, P[, algo_type])
FROM { tb_name | stb_name } [WHERE clause]
```
811

812
**Description**: Similar to `PERCENTILE`, but a simulated result is returned
813

814
**Return value type**: Double precision floating point
815

G
gccgdb1234 已提交
816
**Applicable column types**: Numeric types
817 818 819

**Applicable table types**: table, STable

820
**More explanations**
821

822 823 824
- _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.
825

826
**Nested query**: It can be used in both the outer query and inner query in a nested query.
827

828
### BOTTOM
829

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

834
**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.
835

836
**Return value type**: Same as the column being operated upon
837

G
gccgdb1234 已提交
838
**Applicable column types**: Numeric types
839 840 841 842 843

**Applicable table types**: table, STable

**More explanations**:

844 845 846
- _k_ must be in range [1,100]
- The timestamp associated with the selected values are returned too
- Can't be used with `FILL`
847

848
### FIRST
849

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

854
**Description**: The first non-null value of a specific column in a table or STable
855

856
**Return value type**: Same as the column being operated upon
857

858
**Applicable column types**: Any data type
859 860 861 862 863

**Applicable table types**: table, STable

**More explanations**:

864 865 866
- 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
- A result will NOT be returned if all the columns in the result set are all NULL
867

868
### INTERP
869

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

874
**Description**: The value that matches the specified timestamp range is returned, if existing; or an interpolation value is returned.
875

876
**Return value type**: Same as the column being operated upon
877

878
**Applicable column types**: Numeric data types
879

880
**Applicable table types**: table, STable, nested query
881

882
**More explanations**
883

884 885 886 887 888 889 890
- `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 and a `where` clause 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..
891

892
### LAST
893

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

898
**Description**: The last non-NULL value of a specific column in a table or STable
899

900
**Return value type**: Same as the column being operated upon
901

902
**Applicable column types**: Any data type
903 904 905 906 907

**Applicable table types**: table, STable

**More explanations**:

908 909 910
- 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.
911

912
### LAST_ROW
913

914 915
```
SELECT LAST_ROW(field_name) FROM { tb_name | stb_name };
916 917
```

918
**Description**: The last row of a table or STable
919

920
**Return value type**: Same as the column being operated upon
921

922
**Applicable column types**: Any data type
923 924 925 926 927

**Applicable table types**: table, STable

**More explanations**:

928 929
- 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`.
930

931
### MAX
932

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

937
**Description**: The maximum value of a specific column of a table or STable
938

939
**Return value type**: Same as the data type of the column being operated upon
940

G
gccgdb1234 已提交
941
**Applicable column types**: Numeric types
942 943 944

**Applicable table types**: table, STable

945
### MIN
946

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

951
**Description**: The minimum value of a specific column in a table or STable
952

953
**Return value type**: Same as the data type of the column being operated upon
954

G
gccgdb1234 已提交
955
**Applicable column types**: Numeric types
956

957
**Applicable table types**: table, STable
958

959
### PERCENTILE
960

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

965
**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.
966

967
**Return value type**: Double precision floating point
968

G
gccgdb1234 已提交
969
**Applicable column types**: Numeric types
970

971
**Applicable table types**: table
972

973
**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.
974

975
### TAIL
976

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

981
**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`.
982

983
**Parameter value range**: k: [1,100] offset_val: [0,100]
984

985
**Return value type**: Same as the column being operated upon
986

987
**Applicable column types**: Any data type except form timestamp, i.e. the primary key
988

989
### TOP
990 991

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

995
**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.
996

997
**Return value type**: Same as the column being operated upon
998

G
gccgdb1234 已提交
999
**Applicable column types**: Numeric types
1000 1001 1002 1003 1004

**Applicable table types**: table, STable

**More explanations**:

1005 1006 1007
- _k_ must be in range [1,100]
- The timestamp associated with the selected values are returned too
- Can't be used with `FILL`
1008

1009
### UNIQUE
1010 1011

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

1015
**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.
1016

1017
**Return value type**: Same as the column or tag being operated upon
1018

1019
**Applicable column types**: Any data types except for timestamp
1020

1021
**More explanations**:
1022

1023 1024
- 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.
1025

1026
## Time-Series Specific Functions
1027

1028
TDengine provides a set of time-series specific functions to better meet the requirements in querying time-series data. In general databases, similar functionalities can only be achieved with much more complex syntax and much worse performance. TDengine provides these functionalities in builtin functions so that the burden on user side is minimized.
1029

1030
### CSUM
1031

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

1036
**Description**: The cumulative sum of each row for a specific column. The number of output rows is same as that of the input rows.
1037

1038
**Return value type**: Long integer for integers; Double for floating points. Timestamp is returned for each row.
1039

G
gccgdb1234 已提交
1040
**Applicable data types**: Numeric types
1041 1042 1043 1044 1045

**Applicable table types**: table, STable

**Applicable nested query**: Inner query and Outer query

1046 1047 1048 1049
**More explanations**:
- 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
1050

1051
### DERIVATIVE
1052 1053

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

1057
**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.
1058

1059
**Return value type**: Double precision floating point
1060

G
gccgdb1234 已提交
1061
**Applicable column types**: Numeric types
1062 1063 1064

**Applicable table types**: table, STable

1065
**More explanations**:
1066

1067 1068
- 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.
1069

1070
### DIFF
1071

1072 1073
```sql
SELECT {DIFF(field_name, ignore_negative) | DIFF(field_name)} FROM tb_name [WHERE clause];
1074 1075
```

1076
**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.
1077

1078
**Return value type**: Same as the column being operated upon
1079

G
gccgdb1234 已提交
1080
**Applicable column types**: Numeric types
1081 1082 1083

**Applicable table types**: table, STable

1084
**More explanations**:
1085

1086 1087
- The number of result rows is the number of rows subtracted by one, no output for the first row
- It can be used on STable with `GROUP by tbname`
1088

1089
### IRATE
1090 1091

```
1092
SELECT IRATE(field_name) FROM tb_name WHERE clause;
1093 1094
```

1095
**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.
1096

1097
**Return value type**: Double precision floating number
1098

G
gccgdb1234 已提交
1099
**Applicable column types**: Numeric types
1100 1101 1102

**Applicable table types**: table, STable

1103
**More explanations**:
1104

1105
- It can be used on stble with `GROUP BY`, i.e. timelines generated by `GROUP BY tbname` on a STable.
1106

1107
### MAVG
1108

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

1113
**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 of _k_ is [1,1000].
1114

1115
**Return value type**: Double precision floating point
1116

G
gccgdb1234 已提交
1117
**Applicable data types**: Numeric types
1118 1119 1120

**Applicable nested query**: Inner query and Outer query

1121
**Applicable table types**: table, STable
1122

1123
**More explanations**:
1124

1125 1126 1127
- Arithmetic operation can't be performed on the result of `MAVG`.
- 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.
1128

1129
### SAMPLE
1130

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

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

1137
**Return value type**: Same as the column being operated plus the associated timestamp
1138

1139
**Applicable data types**: Any data type except for tags of STable
1140 1141 1142 1143 1144 1145 1146

**Applicable table types**: table, STable

**Applicable nested query**: Inner query and Outer query

**More explanations**:

1147 1148
- 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
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164

### STATECOUNT

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

**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**:

- 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

**Return value type**: Integer

G
gccgdb1234 已提交
1165
**Applicable data types**: Numeric types
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191

**Applicable table types**: table, STable

**Applicable nested query**: Outer query only

**More explanations**:

- 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

### STATEDURATION

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

**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.

**Applicable parameter values**:

- 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

**Return value type**: Integer

G
gccgdb1234 已提交
1192
**Applicable data types**: Numeric types
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

**Applicable table types**: table, STable

**Applicable nested query**: Outer query only

**More explanations**:

- 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

1203
### TWA
1204 1205

```
1206
SELECT TWA(field_name) FROM tb_name WHERE clause;
1207 1208
```

1209
**Description**: Time weighted average on a specific column within a time range
1210

1211
**Return value type**: Double precision floating number
1212

G
gccgdb1234 已提交
1213
**Applicable column types**: Numeric types
1214 1215 1216 1217 1218

**Applicable table types**: table, STable

**More explanations**:

1219
- It can be used on stable with `GROUP BY`, i.e. timelines generated by `GROUP BY tbname` on a STable.
1220

1221
## System Information Functions
1222

1223
### DATABASE
1224 1225

```
1226
SELECT DATABASE();
1227 1228
```

1229
**Description**:Return the current database being used. If the user doesn't specify database when logon and doesn't use `USE` SQL command to switch the datbase, this function returns NULL.
1230

1231
### CLIENT_VERSION
1232 1233

```
1234
SELECT CLIENT_VERSION();
1235 1236
```

1237
**Description**:Return the client version.
1238

1239
### SERVER_VERSION
1240 1241

```
1242
SELECT SERVER_VERSION();
1243 1244
```

1245
**Description**:Returns the server version.
1246

1247
### SERVER_STATUS
1248 1249

```
1250
SELECT SERVER_VERSION();
1251 1252
```

1253
**Description**:Returns the server's status.