10-function.md 39.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 value of a specific field.
19

20
**Return value type**: Same as input type.
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
**More explanations**:
29
- Cannot 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 field.
38

39
**Return value type**: DOUBLE.
40

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

43
**Applicable table types**: table, STable.
44

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

47
**More explanations**:
48
- Cannot 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 field.
57

58
**Return value type**: DOUBLE.
59

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

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

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

66
**More explanations**:
67
- Cannot 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 field.
76

77
**Return value type**: DOUBLE.
78 79

**Applicable data types**: Numeric types.
80

S
Sean Ely 已提交
81
**Applicable table types**: table, STable
82

83
**Applicable nested query**: Inner query and Outer query.
84

85
**More explanations**:
86
- Cannot be used with aggregate functions.
87

88
#### CEIL
89 90

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

94
**Description**: The rounded up value of a specific field.
95

96
**Return value type**: Same as input type.
97

98
**Applicable data types**: Numeric types.
99

S
Sean Ely 已提交
100
**Applicable table types**: table, STable
101

102
**Applicable nested query**: Inner query and outer query.
103

104
**More explanations**:
105
- Can't be used with aggregate functions.
106

107
#### COS
108

109 110
```sql
SELECT COS(field_name) FROM { tb_name | stb_name } [WHERE clause]
111 112
```

113
**Description**: The cosine of a specific field.
114

115
**Return value type**: DOUBLE.
116 117 118

**Applicable data types**: Numeric types.

119
**Applicable table types**: table, STable.
120

121
**Applicable nested query**: Inner query and Outer query.
122 123

**More explanations**:
124
- Can't be used with aggregate functions.
125

126
#### FLOOR
127 128

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

132
**Description**: The rounded down value of a specific field.
133

134
**More explanations**: Refer to `CEIL` function for usage restrictions.
135 136 137 138 139

#### LOG

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

142
**Description**: The logarithm of a specific field with `base` as the radix. If `base` parameter is ignored, natural logarithm of the field is returned.
143

144
**Return value type**: DOUBLE.
145

146
**Applicable data types**: Numeric types.
147

148
**Applicable table types**: table, STable.
149

150
**Applicable nested query**: Inner query and Outer query.
151

152 153
**More explanations**:
- Can't be used with aggregate functions
154

155
#### POW
156

157 158 159
```sql
SELECT POW(field_name, power) FROM { tb_name | stb_name } [WHERE clause]
```
160

161
**Description**: The power of a specific field with `power` as the index.
162

163
**Return value type**: DOUBLE.
164

165
**Applicable data types**: Numeric types.
166

167
**Applicable table types**: table, STable.
168

169
**Applicable nested query**: Inner query and Outer query.
170

171
**More explanations**:
172
- Can't be used with aggregate functions.
173

174
#### ROUND
175 176

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

180
**Description**: The rounded value of a specific field.
181

182
**More explanations**: Refer to `CEIL` function for usage restrictions.
183

184
#### SIN
185

186 187 188
```sql
SELECT SIN(field_name) FROM { tb_name | stb_name } [WHERE clause]
```
189

190
**Description**: The sine of a specific field.
191

192
**Description**: The anti-cosine of a specific field.
193

194
**Return value type**: DOUBLE.
195

196
**Applicable data types**: Numeric types.
197

198
**Applicable table types**: table, STable.
199

200
**Applicable nested query**: Inner query and Outer query.
201

202
**More explanations**:
203
- Can't be used with aggregate functions.
204

205
#### SQRT
206

207 208 209
```sql
SELECT SQRT(field_name) FROM { tb_name | stb_name } [WHERE clause]
```
210

211
**Description**: The square root of a specific field.
212

213
**Return value type**: DOUBLE.
214

215
**Applicable data types**: Numeric types.
G
gccgdb1234 已提交
216

217
**Applicable table types**: table, STable.
G
gccgdb1234 已提交
218

219
**Applicable nested query**: Inner query and Outer query.
220

221
**More explanations**:
222
- Can't be used with aggregate functions.
G
gccgdb1234 已提交
223

224
#### TAN
225

226 227
```sql
SELECT TAN(field_name) FROM { tb_name | stb_name } [WHERE clause]
228 229
```

230
**Description**: The tangent of a specific field.
G
gccgdb1234 已提交
231

232
**Description**: The anti-cosine of a specific field.
G
gccgdb1234 已提交
233

234
**Return value type**: DOUBLE.
G
gccgdb1234 已提交
235

236
**Applicable data types**: Numeric types.
G
gccgdb1234 已提交
237

238
**Applicable table types**: table, STable.
G
gccgdb1234 已提交
239

240
**Applicable nested query**: Inner query and Outer query.
G
gccgdb1234 已提交
241

242
**More explanations**:
243
- Can't be used with aggregate functions.
G
gccgdb1234 已提交
244

245
### String Functions
246

247
String functiosn take strings as input and output numbers or strings.
248

249
#### CHAR_LENGTH
250 251

```
252
SELECT CHAR_LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
253 254
```

255
**Description**: The mumber of characters of a string.
256

257
**Return value type**: INTEGER.
258

259
**Applicable data types**: VARCHAR, NCHAR.
260

261
**Applicable table types**: table, STable.
262

263
**Applicable nested query**: Inner query and Outer query.
264 265


266
#### CONCAT
267

268 269
```sql
SELECT CONCAT(str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
270 271
```

272
**Description**: The concatenation result of two or more strings.
273

274
**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. If input strings contain NULL value, the result is NULL. 
275

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

278 279 280
**Applicable table types**: table, STable.

**Applicable nested query**: Inner query and Outer query.
281 282


283
#### CONCAT_WS
284 285

```
286
SELECT CONCAT_WS(separator, str1|column1, str2|column2, ...) FROM { tb_name | stb_name } [WHERE clause]
287 288
```

289
**Description**: The concatenation result of two or more strings with separator.
290

291
**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. If input strings contain NULL value, the result is NULL. 
292

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

295
**Applicable table types**: table, STable.
296

297
**Applicable nested query**: Inner query and Outer query.
298 299


300
#### LENGTH
301 302

```
303
SELECT LENGTH(str|column) FROM { tb_name | stb_name } [WHERE clause]
304 305
```

306
**Description**: The length in bytes of a string.
307

308
**Return value type**: INTEGER.
309

310
**Applicable data types**: VARCHAR, NCHAR.
311

312
**Applicable table types**: table, STable.
313

314
**Applicable nested query**: Inner query and Outer query.
315 316


317
#### LOWER
318 319

```
320
SELECT LOWER(str|column) FROM { tb_name | stb_name } [WHERE clause]
321 322
```

323
**Description**: Convert the input string to lower case.
324

325
**Return value type**: Same as input type.
326

327
**Applicable data types**: VARCHAR, NCHAR.
328

329
**Applicable table types**: table, STable.
330

331
**Applicable nested query**: Inner query and Outer query.
332 333


334
#### LTRIM
335 336

```
337
SELECT LTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
338 339
```

340
**Description**: Remove the left leading blanks of a string.
341

342
**Return value type**: Same as input type.
343

344
**Applicable data types**: VARCHAR, NCHAR.
345

346
**Applicable table types**: table, STable.
347

348
**Applicable nested query**: Inner query and Outer query.
349 350


351
#### RTRIM
352 353

```
354
SELECT RTRIM(str|column) FROM { tb_name | stb_name } [WHERE clause]
355 356
```

357
**Description**: Remove the right tailing blanks of a string.
358

359
**Return value type**: Same as input type.
360

361
**Applicable data types**: VARCHAR, NCHAR.
362

363
**Applicable table types**: table, STable.
364

365
**Applicable nested query**: Inner query and Outer query.
366 367


368
#### SUBSTR
369 370

```
371
SELECT SUBSTR(str,pos[,len]) FROM { tb_name | stb_name } [WHERE clause]
372 373
```

374
**Description**: The sub-string starting from `pos` with length of `len` from the original string `str`.
375

376
**Return value type**: Same as input type.
377

378
**Applicable data types**: VARCHAR, NCHAR.
379

380
**Applicable table types**: table, STable.
381

382
**Applicable nested query**: Inner query and Outer query.
383

384
**More explanations**:
385

386 387
- 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.
388
- If `len` is not specified, it means from `pos` to the end of string.
389

390
#### UPPER
391 392

```
393
SELECT UPPER(str|column) FROM { tb_name | stb_name } [WHERE clause]
394 395
```

396
**Description**: Convert the input string to upper case.
397

398
**Return value type**: Same as input type.
399

400
**Applicable data types**: VARCHAR, NCHAR.
401

402
**Applicable table types**: table, STable.
403

404
**Applicable nested query**: Inner query and Outer query.
405 406


407
### Conversion Functions
408

409
Conversion functions convert from one data type to another.
410

411
#### CAST
412

413 414
```sql
SELECT CAST(expression AS type_name) FROM { tb_name | stb_name } [WHERE clause]
415 416
```

417
**Description**: Used for type casting. Convert `expression` to the type specified by `type_name`.
418

419
**Return value type**: The type specified by parameter `type_name`.
420

421
**Applicable data types**: `expression` can be any data type except for JSON.
422

423
**More explanations**:
424

425
- Error will be reported for unsupported type casting.
426
- Some values of some supported data types may not be casted, below are known issues:
G
gccgdb1234 已提交
427
  1)When casting VARCHAR/NCHAR to BIGINT/BIGINT UNSIGNED, some characters may be treated as illegal, for example "a" may be converted to 0.
428 429
  2)When casting to numeric type, if converted result is out of range the destination data type can hold, overflow may occur and casting behavior is undefined.
  3) When casting to VARCHAR/NCHAR type, if converted string length exceeds the length specified in `type_name`, the result will be truncated. (e.g. CAST("abcd" as BINARY(2)) will return string "ab").
430

431
#### TO_ISO8601
432

433
```sql
434
SELECT TO_ISO8601(ts[, timezone]) FROM { tb_name | stb_name } [WHERE clause];
435 436
```

437
**Description**: The ISO8601 date/time format converted from a UNIX timestamp, with timezone attached. `timezone` parameter allows attaching any customized timezone string to the output format. If `timezone` parameter is not specified, the timezone information of client side system will be attached.
438

439
**Return value type**: VARCHAR.
440

441
**Applicable data types**: INTEGER, TIMESTAMP.
442

443
**Applicable table types**: table, STable.
444

445
**More explanations**:
446

447 448
- If the input is INTEGER represents UNIX timestamp, the precision of the returned value is determined by the digits of the input integer.
- If the input is of TIMESTAMP type, The precision of the returned value is same as the precision set for the current database in use.
449

450
#### TO_JSON
451

452 453
```sql
SELECT TO_JSON(str_literal) FROM { tb_name | stb_name } [WHERE clause];
454 455
```

456
**Description**: Convert a JSON string to a JSON body.
457

458
**Return value type**: JSON.
459

460
**Applicable data 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.
461

462
**Applicable table types**: table, STable.
463

464
**Applicable nested query**: Inner query and Outer query.
465

466
#### TO_UNIXTIMESTAMP
467

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

472
**Description**: UNIX timestamp converted from a string of date/time format.
473

474
**Return value type**: BIGINT.
475

476
**Applicable data types**: VARCHAR, NCHAR.
477

478
**Applicable table types**: table, STable.
479

480
**More explanations**:
481

482 483
- The input string must be compatible with ISO8601/RFC3339 standard, NULL will be returned if the string cannot be converted.
- The precision of the returned timestamp is same as the precision set for the current database in use.
484

485
### DateTime Functions
486

487
DateTime functions applied to timestamp data. NOW(), TODAY() and TIMEZONE() are executed only once even though they may occur multiple times in a single SQL statement.
488

489
#### NOW
490

491 492 493 494 495
```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(), ...);
```
496

497
**Description**: The current time of the client side system.
498

499
**Return value type**: TIMESTAMP.
500

501
**Applicable data types**: TIMESTAMP only if used in WHERE/INSERT clause.
502

503
**Applicable table types**: table, STable.
504

505
**More explanations**:
506

507 508 509
- Addition and Subtraction operation with time duration can be performed, for example NOW() + 1s, the time unit can be one of the followings:
  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 database in use.
510

511 512 513
#### TIMEDIFF

```sql
514
SELECT TIMEDIFF(ts1 | datetime_string1, ts2 | datetime_string2 [, time_unit]) FROM { tb_name | stb_name } [WHERE clause];
515 516
```

517
**Description**: The difference(duration) between two timestamps, and rounded to the time unit specified by `time_unit`.
518

519
**Return value type**: BIGINT.
520

521
**Applicable data types**: INTEGER/TIMESTAMP represents UNIX timestamp, or VARCHAR/NCHAR string in date/time format.
522

523
**Applicable table types**: table, STable.
524 525 526

**More explanations**:

527
- Time unit specified by `time_unit` can be:
528 529
  1b(nanosecond), 1u(microsecond),1a(millisecond),1s(second),1m(minute),1h(hour),1d(day),1w(week).
- If `time_unit` parameter is not specified, the precision of the returned time duration is same as the precision set for the current database in use.
530

531
#### TIMETRUNCATE
532 533

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

537
**Description**: Truncate the input timestamp with unit specified by `time_unit`.
538

539
**Return value type**: TIMESTAMP.
540

541
**Applicable data types**: INTEGER/TIMESTAMP represents UNIX timestamp, or VARCHAR/NCHAR string in date/time format.
542

543
**Applicable table types**: table, STable.
544 545 546

**More explanations**:

547
- Time unit specified by `time_unit` can be:
548 549
  1b(nanosecond),1u(microsecond),1a(millisecond),1s(second),1m(minute),1h(hour),1d(day),1w(week).
- The precision of the returned timestamp is same as the precision set for the current database in use.
550

551
#### TIMEZONE
552 553

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

557
**Description**: The timezone of the client side system.
558

559
**Return value type**: VARCHAR.
560

561
**Applicable data types**: None.
562

563
**Applicable table types**: table, STable.
564

565
#### TODAY
566

567 568 569 570
```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(), ...);
571 572
```

573
**Description**: The timestamp of 00:00:00 of the client side system.
574

575
**Return value type**: TIMESTAMP.
576

577
**Applicable data types**: TIMESTAMP only if used in WHERE/INSERT clause.
578

579
**Applicable table types**: table, STable.
580

581
**More explanations**:
582

583 584 585
- Addition and Subtraction operation can be performed with time durations, 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 database in use.
586

587
## Aggregate Functions
588

589
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.
590

G
Ganlin Zhao 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
### APERCENTILE

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

**Description**: Similar to `PERCENTILE`, but a approximated result is returned.

**Return value type**: DOUBLE.

**Applicable column types**: Numeric types.

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

**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.
- **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")`.
- If `default` is used, histogram based algorithm is used for calculation. If `t-digest` is used, `t-digest` sampling algorithm is used to calculate the result.

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

614
### AVG
615 616

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

620
**Description**: Get the average value of a column in a table or STable.
621

622
**Return value type**: DOUBLE.
623

624
**Applicable data types**: Numeric type.
625

626
**Applicable table types**: table, STable.
627

628
### COUNT
629 630

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

634
**Description**: Get the number of rows in a table or a super table.
635

636
**Return value type**: BIGINT.
637

638
**Applicable data types**: All data types.
639

640
**Applicable table types**: table, STable.
641

642
**More explanation**:
643

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

647
### ELAPSED
648

649 650
```mysql
SELECT ELAPSED(field_name[, time_unit]) FROM { tb_name | stb_name } [WHERE clause] [INTERVAL(interval [, offset]) [SLIDING sliding]];
651 652
```

653
**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 duration within the specified time range. Please be noted that the calculated time duration is in the specified `time_unit`.
654

655
**Return value type**:DOUBLE.
656

657
**Applicable Column type**:TIMESTAMP.
658

659
**Applicable tables**: table, STable, outter in nested query.
660

661
**Explanations**
662

663 664 665 666 667 668 669
- `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.
670
- It cannot be used with `leastsquares`, `diff`, `derivative`, `top`, `bottom`, `last_row`, `interp`.
671

672
### LEASTSQUARES
673

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

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

680
**Return value type**: VARCHAR string in the format of "(slope, intercept)".
681

682 683 684
**Applicable data types**: Numeric types.

**Applicable table types**: table only.
685

686
### SPREAD
687

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

692
**Description**: The difference between the max and the min value of a specific column.
693

694
**Return value type**: DOUBLE.
695

696
**Applicable column types**: Numeric types.
697

698
**Applicable table types**: table, STable.
699

700
**More explanations**: Can be used on a column of TIMESTAMP type, the result time unit precision is same as the current database in use.
701

702
### STDDEV
703

704 705
```
SELECT STDDEV(field_name) FROM tb_name [WHERE clause];
706 707
```

708
**Description**: Standard deviation of a specific column in a table or STable.
709

710
**Return value type**: DOUBLE.
711

712
**Applicable column types**: Numeric types.
713

714
**Applicable table types**: table, STable.
715

716
### SUM
717

718 719 720
```
SELECT SUM(field_name) FROM tb_name [WHERE clause];
```
721

722
**Description**: The summation of values of a specific column in a table or STable.
723

724
**Return value type**: DOUBLE.
725

726
**Applicable column types**: Numeric types.
727

728
**Applicable table types**: table, STable.
729

730
### HYPERLOGLOG
731

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

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

738
**Return value type**: INTEGER.
739

740
**Applicable column types**: All data types.
741

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

744
### HISTOGRAM
745

746 747 748
```
SELECT HISTOGRAM(field_name,bin_type, bin_description, normalized) FROM tb_name [WHERE clause];
```
749

750
**Description**:Returns count of data points in user-specified ranges.
751

752
**Return value type**:DOUBLE or BIGINT, depends on normalized parameter settings.
753

754
**Applicable column type**:Numerical types.
755

756
**Applicable table types**: table, STable.
757

758
**Explanations**
759

760
1. bin_type: parameter to indicate the bucket type, valid inputs are: "user_input", "linear_bin", "log_bin"。
G
Ganlin Zhao 已提交
761
2. bin_description: parameter to describe the rule to generate buckets,can be in the following JSON formats for each bin_type respectively:
762

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

765 766 767 768 769 770
   - "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].
771

772 773 774 775 776 777
   - "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].
778

779
3. normalized: setting to 1/0 to turn on/off result normalization.
780

G
Ganlin Zhao 已提交
781
### PERCENTILE
782

783
```
G
Ganlin Zhao 已提交
784
SELECT PERCENTILE(field_name, P) FROM { tb_name } [WHERE clause];
785
```
786

G
Ganlin Zhao 已提交
787
**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.
788

789
**Return value type**: DOUBLE.
790

791
**Applicable column types**: Numeric types.
792

G
Ganlin Zhao 已提交
793
**Applicable table types**: table.
794

G
Ganlin Zhao 已提交
795
**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.
796

G
Ganlin Zhao 已提交
797
## Selector Functions
798

G
Ganlin Zhao 已提交
799
Selector functiosn choose one or more rows in the query result according to the semantics. You can specify to output primary timestamp column and other columns including tbname and tags so that you can easily know which rows the selected values belong to.
800

801
### BOTTOM
802

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

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

809
**Return value type**: Same as the column being operated upon.
810

811
**Applicable column types**: Numeric types.
812

813
**Applicable table types**: table, STable.
814 815 816

**More explanations**:

817 818 819
- _k_ must be in range [1,100].
- The timestamp associated with the selected values are returned too.
- Can't be used with `FILL`.
820

821
### FIRST
822

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

827
**Description**: The first non-null value of a specific column in a table or STable.
828

829
**Return value type**: Same as the column being operated upon.
830

831
**Applicable column types**: All data types.
832

833
**Applicable table types**: table, STable.
834 835 836

**More explanations**:

837 838 839
- 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
840

841
### INTERP
842

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

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

849
**Return value type**: Same as the column being operated upon.
850

851
**Applicable column types**: Numeric data types.
852

853
**Applicable table types**: table, STable, nested query.
854

855
**More explanations**
856

857 858 859 860 861 862 863
- `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..
864

865
### LAST
866

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

871
**Description**: The last non-NULL value of a specific column in a table or STable.
872

873
**Return value type**: Same as the column being operated upon.
874

875
**Applicable column types**: All data types.
876

877
**Applicable table types**: table, STable.
878 879 880

**More explanations**:

881 882 883
- 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.
884

885
### LAST_ROW
886

887 888
```
SELECT LAST_ROW(field_name) FROM { tb_name | stb_name };
889 890
```

891
**Description**: The last row of a table or STable.
892

893
**Return value type**: Same as the column being operated upon.
894

895
**Applicable column types**: All data type.
896

897
**Applicable table types**: table, STable.
898 899 900

**More explanations**:

901
- 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.
902
- Cannot be used with `INTERVAL`.
903

904
### MAX
905

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

910
**Description**: The maximum value of a specific column of a table or STable.
911

912
**Return value type**: Same as the data type of the column being operated upon.
913

914
**Applicable column types**: Numeric types.
915

916
**Applicable table types**: table, STable.
917

918
### MIN
919

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

924
**Description**: The minimum value of a specific column in a table or STable.
925

926
**Return value type**: Same as the data type of the column being operated upon.
927

928
**Applicable column types**: Numeric types.
929

930
**Applicable table types**: table, STable.
931

G
Ganlin Zhao 已提交
932
### MODE
933

934
```
G
Ganlin Zhao 已提交
935
SELECT MODE(field_name) FROM tb_name [WHERE clause];
936
```
937

G
Ganlin Zhao 已提交
938
**Description**:The value which has the highest frequency of occurrence. NULL is returned if there are multiple values which have highest frequency of occurrence.
939

G
Ganlin Zhao 已提交
940
**Return value type**:Same as the data type of the column being operated upon.
941

G
Ganlin Zhao 已提交
942
**Applicable column types**: All data types.
943

G
Ganlin Zhao 已提交
944
**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.
945

G
Ganlin Zhao 已提交
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
### SAMPLE

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

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

**Return value type**: Same as the column being operated.

**Applicable data types**: All data types.

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

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

**More explanations**:

- Arithmetic operation cannot be operated on the result of `SAMPLE` function
- Must be used with `Partition by tbname` when it's used on a STable to force the result on each single timeline.
966

967
### TAIL
968

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

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

975
**Parameter value range**: k: [1,100] offset_val: [0,100].
976

977
**Return value type**: Same as the column being operated upon.
978

979
**Applicable column types**: All data types.
980

981
### TOP
982 983

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

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

989
**Return value type**: Same as the column being operated upon.
990

991
**Applicable column types**: Numeric types.
992

993
**Applicable table types**: table, STable.
994 995 996

**More explanations**:

997 998 999
- _k_ must be in range [1,100].
- The timestamp associated with the selected values are returned too.
- Cannot be used with `FILL`.
1000

1001
### UNIQUE
1002 1003

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

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

1009
**Return value type**: Same as the column or tag being operated upon.
1010

1011
**Applicable column types**: All data types.
1012

1013
**More explanations**:
1014

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

1018
## Time-Series Specific Functions
1019

1020
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.
1021

1022
### CSUM
1023

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

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

1030
**Return value type**: BIGINT for signed integer input types; UNSIGNED BIGINT for unsigned integer input types; DOUBLE for floating point input types. 
1031

1032
**Applicable data types**: Numeric types.
1033

1034
**Applicable table types**: table, STable.
1035

1036
**Applicable nested query**: Inner query and Outer query.
1037

1038
**More explanations**:
1039 1040 1041
- Arithmetic operation cannot be performed on the result of `csum` function.
- Can only be used with aggregate functions.
- `Partition by tbname` must be used together on a STable to force the result on a single timeline.
1042

1043
### DERIVATIVE
1044 1045

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

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

1051
**Return value type**: DOUBLE.
1052

1053
**Applicable column types**: Numeric types.
1054

1055
**Applicable table types**: table, STable.
1056

1057
**More explanations**:
1058

1059
- The number of result rows is the number of total rows in the time range subtracted by one, no output for the first row.
1060
- It can be used together with `PARTITION BY tbname` against a STable.
G
Ganlin Zhao 已提交
1061
- Can be used together with selection of relative columns. E.g. select \_rowts, DERIVATIVE() from.
1062

1063
### DIFF
1064

1065 1066
```sql
SELECT {DIFF(field_name, ignore_negative) | DIFF(field_name)} FROM tb_name [WHERE clause];
1067 1068
```

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

1071
**Return value type**: Same as the column being operated upon.
1072

1073
**Applicable column types**: Numeric types.
1074

1075
**Applicable table types**: table, STable.
1076

1077
**More explanations**:
1078

1079
- The number of result rows is the number of rows subtracted by one, no output for the first row.
G
Ganlin Zhao 已提交
1080
- It can be used on STable with `PARTITION by tbname`.
G
Ganlin Zhao 已提交
1081
- Can be used together with selection of relative columns. E.g. select \_rowts, DIFF() from.
1082

1083
### IRATE
1084 1085

```
1086
SELECT IRATE(field_name) FROM tb_name WHERE clause;
1087 1088
```

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

1091
**Return value type**: DOUBLE.
1092

1093
**Applicable column types**: Numeric types.
1094

1095
**Applicable table types**: table, STable.
1096

1097
**More explanations**:
1098

1099
- It can be used on stble with `PARTITION BY`, i.e. timelines generated by `PARTITION BY tbname` on a STable.
1100

1101
### MAVG
1102

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

1107
**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].
1108

1109
**Return value type**: DOUBLE.
1110

1111
**Applicable data types**: Numeric types.
1112

1113
**Applicable nested query**: Inner query and Outer query.
1114

1115
**Applicable table types**: table, STable.
1116

1117
**More explanations**:
1118

1119 1120 1121
- Arithmetic operation cannot be performed on the result of `MAVG`.
- Cannot be used with aggregate functions.
- Must be used with `PARTITION BY tbname` when it's used on a STable to force the result on each single timeline.
1122 1123 1124 1125 1126 1127 1128

### STATECOUNT

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

1129
**Description**: The number of continuous rows satisfying the specified conditions for a specific column. 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.
1130 1131 1132

**Applicable parameter values**:

1133 1134
- 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).
- val : Numeric types.
1135

1136
**Return value type**: INTEGER.
1137

1138
**Applicable data types**: Numeric types.
1139

1140
**Applicable table types**: table, STable.
1141

1142
**Applicable nested query**: Outer query only.
1143 1144 1145

**More explanations**:

1146 1147
- Must be used together with `PARTITION BY tbname` when it's used on a STable to force the result into each single timeline.
- Cannot be used with window operation, like interval/state_window/session_window.
1148 1149 1150 1151 1152 1153 1154

### STATEDURATION

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

1155
**Description**: The length of time range in which all rows satisfy the specified condition for a specific column. 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.
1156 1157 1158

**Applicable parameter values**:

1159
- 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).
G
Ganlin Zhao 已提交
1160 1161
- val : Numeric types.
- unit : The unit of time interval, can be: 1b(nanosecond), 1u(microsecond),1a(millisecond),1s(second),1m(minute),1h(hour),1d(day),1w(week). If not specified, default is same as the current database time precision in use.
1162

1163
**Return value type**: INTEGER.
1164

1165
**Applicable data types**: Numeric types.
1166

1167
**Applicable table types**: table, STable.
1168

1169
**Applicable nested query**: Outer query only.
1170 1171 1172

**More explanations**:

G
Ganlin Zhao 已提交
1173 1174
- Must be used together with `PARTITION BY tbname` when it's used on a STable to force the result into each single timeline.
- Cannot be used with window operation, like interval/state_window/session_window.
1175

1176
### TWA
1177 1178

```
1179
SELECT TWA(field_name) FROM tb_name WHERE clause;
1180 1181
```

1182
**Description**: Time weighted average on a specific column within a time range.
1183

1184
**Return value type**: DOUBLE.
1185

1186
**Applicable column types**: Numeric types.
1187

1188
**Applicable table types**: table, STable.
1189 1190 1191

**More explanations**:

1192
- It can be used on stable with `PARTITION BY`, i.e. timelines generated by `PARTITION BY tbname` on a STable.
1193

1194
## System Information Functions
1195

1196
### DATABASE
1197 1198

```
1199
SELECT DATABASE();
1200 1201
```

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

1204
### CLIENT_VERSION
1205 1206

```
1207
SELECT CLIENT_VERSION();
1208 1209
```

1210
**Description**:Return the client version.
1211

1212
### SERVER_VERSION
1213 1214

```
1215
SELECT SERVER_VERSION();
1216 1217
```

1218
**Description**:Returns the server version.
1219

1220
### SERVER_STATUS
1221 1222

```
1223
SELECT SERVER_VERSION();
1224 1225
```

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