hyperdb-mcp 0.6.1

MCP server for Hyper database — instant SQL analytics for LLM workflows
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
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
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
# Salesforce Data Cloud SQL Reference

_Scraped from: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/data-cloud-sql-context.html_

_To refresh: `uv run --with playwright python3 scripts/scrape_dc_sql_reference.py`_



---

# Data 360 SQL and Tableau Hyper API | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/data-cloud-sql-context.html_

## Data 360 SQL Reference

As of October 14, 2025, Data Cloud has been rebranded to Data 360. During this transition, you may see references to Data Cloud in our application and documentation. While the name is new, the functionality and content remains unchanged.

Note

Data 360 SQL supports the Data 360 Query API and the Tableau Hyper API. This reference guide describes Data 360 SQL and how it applies to both APIs.

Use Data 360 SQL to query data in Data 360, answer analytical questions, or derive insights from your data.

Data 360 SQL works with these resources.

- Integrated apps such as Query Editor, DBeaver, or Tableau

- Custom apps built with language-specific libraries on Data 360

For more information, see How Do I Query Data in Data 360? and Data 360 SQL Query APIs.

Use Tableau Hyper API to access data in Tableau for analytics and data transformations.

Tableau Hyper API uses Data 360 SQL to complete these tasks.

- Make data in unsupported data sources accessible to Tableau.

- Automate custom extract, transform, and load (ETL) processes.

- Implement rolling window updates or custom incremental updates.

For more information, see the Tableau Hyper API documentation.



---

# Data 360 SQL Syntax | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/sql-commands-context.html_

## Data 360 SQL Syntax

This section describes the standard syntax for creating Data 360 SQL statements and the syntax for the read-only statements available in Data 360 and Tableau Hyper API.

- Syntax

- SELECT

- VALUES



---

# Data Types | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/datatypes.html_

## Data Types

The Data 360 SQL type system includes two groups of types.

- Atomic types that describe single values

- Composite types that describe collections of values

Atomic types are basic data types. This table lists all available atomic types.

Name

Aliases

Description

BIGINT

```
BIGINT
```

INT8

```
INT8
```

Signed eight-byte integer

BOOLEAN

```
BOOLEAN
```

BOOL

```
BOOL
```

Boolean value with ternary logic (true/false/unknown)

BYTES

```
BYTES
```

Binary data (“byte array”)

CHARACTER [ (n) ]

```
CHARACTER [ (n) ]
```

CHAR [ (n) ]

```
CHAR [ (n) ]
```

Fixed-length character string

CHARACTER VARYING (n)

```
CHARACTER VARYING (n)
```

VARCHAR (n)

```
VARCHAR (n)
```

Variable-length character string with limit

DATE

```
DATE
```

Calendar date (year, month, day)

REAL

```
REAL
```

FLOAT4

```
FLOAT4
```

Single-precision floating-point number (4 bytes)

DOUBLE PRECISION

```
DOUBLE PRECISION
```

FLOAT, FLOAT8

```
FLOAT
```

```
FLOAT8
```

Double-precision floating-point number (8 bytes)

INTEGER

```
INTEGER
```

INT, INT4

```
INT
```

```
INT4
```

Signed four-byte integer

INTERVAL

```
INTERVAL
```

Time span, not supported in Tableau

NUMERIC [ (p, s) ]

```
NUMERIC [ (p, s) ]
```

DECIMAL [ (p, s) ]

```
DECIMAL [ (p, s) ]
```

Exact numeric of selectable precision

SMALLINT

```
SMALLINT
```

INT2

```
INT2
```

Signed two-byte integer

TEXT

```
TEXT
```

Variable-length character string

TIME [ WITHOUT TIME ZONE ]

```
TIME [ WITHOUT TIME ZONE ]
```

Time of day (no time zone)

TIMESTAMP [ WITHOUT TIME ZONE ]

```
TIMESTAMP [ WITHOUT TIME ZONE ]
```

Date and time (no time zone)

TIMESTAMP WITH TIME ZONE

```
TIMESTAMP WITH TIME ZONE
```

TIMESTAMPTZ

```
TIMESTAMPTZ
```

Date and time, including time zone

Persisting NUMERIC values into .hyper files with a precision greater than 18 by using Tableau Hyper API requires at least database version 3.

```
NUMERIC
```

```
.hyper
```

Persisting 32-bit floating point values into .hyper files by using Tableau Hyper API requires at least database version 4.

```
.hyper
```

Note

Composite types are collections of multiple data items in a single SQL value. You can use them for dedicated schema denormalization, which is useful for specific domains, such as machine learning applications. Data 360 SQL supports the array composite type.

An array is an ordered sequence of values. Arrays in Data 360 SQL are strongly typed and can be built from all supported atomic types. For more details, see Array Type.



---

# Scalar Functions | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/scalar-functions.html_

## Scalar Functions and Operators

This section describes functions and operators for the built-in data types.



---

# Aggregate Functions | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/aggregate.html_

## Aggregate Functions

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Functions that compute a single result from a set of input values. Use aggregate functions to:

- Summarize data by computing counts, sums, and averages.

- Analyze distributions with statistical functions like standard deviation and variance.

- Find extremes with MIN and MAX values.

- Compute percentiles and identify modes in datasets.

- NULL handling: Except for COUNT, aggregate functions return NULL if no rows are selected. Use COALESCE to substitute a default value.

```
COUNT
```

```
COALESCE
```

- GROUP BY requirement: Non-aggregated fields in the SELECT clause must appear in the GROUP BY clause.

```
SELECT
```

```
GROUP BY
```

- FILTER clause: Use the FILTER clause to apply conditions to individual aggregates.

Common aggregate functions for summarizing data:

- ANY_VALUE - Return an arbitrary value from the group.

- APPROX_COUNT_DISTINCT - Approximate count of distinct values.

- AVG - Calculate the arithmetic mean.

- BIT_AND - Compute bitwise AND of all values.

- BIT_OR - Compute bitwise OR of all values.

- BOOL_AND - True if all values are true.

- BOOL_OR - True if any value is true.

- COUNT - Count rows or non-null values.

- EVERY - True if all values are true (alias for BOOL_AND).

- MAX - Find the maximum value.

- MIN - Find the minimum value.

- SUM - Calculate the total.

Functions for statistical analysis:

- CORR - Compute correlation coefficient.

- COVAR_POP - Compute population covariance.

- COVAR_SAMP - Compute sample covariance.

- STDDEV/STDDEV_SAMP - Compute sample standard deviation.

- STDDEV_POP - Compute population standard deviation.

- VAR_POP - Compute population variance.

- VARIANCE/VAR_SAMP - Compute sample variance.

Cast the input of an aggregate function to force a different output type. For example, use VAR_POP(CAST(A AS DOUBLE PRECISION)) to get a double precision result.

```
VAR_POP(CAST(A AS DOUBLE PRECISION))
```

```
double precision
```

Tip

Aggregate functions that use the WITHIN GROUP syntax:

```
WITHIN GROUP
```

- MODE - Return the most frequent value.

- PERCENTILE_CONT - Compute continuous percentile with interpolation.

- PERCENTILE_DISC - Compute discrete percentile without interpolation.

All ordered-set aggregates ignore NULL values in their sorted input. The fraction parameter must be between 0 and 1.

- GROUPING - Identify which columns are aggregated in grouping sets.

- FILTER Clause - Filter rows passed to aggregate functions.

Calculate summary statistics.

```
1SELECT
2    count(*) AS total_orders,
3    sum(amount) AS total_revenue,
4    avg(amount) AS avg_order_value,
5    min(amount) AS min_order,
6    max(amount) AS max_order
7FROM orders;
```

Summarize data by category.

```
1SELECT
2    category,
3    count(*) AS product_count,
4    avg(price) AS avg_price
5FROM products
6GROUP BY category;
```

Compute multiple filtered aggregates in one query.

```
1SELECT
2    count(*) AS total,
3    count(*) FILTER (WHERE status = 'completed') AS completed,
4    count(*) FILTER (WHERE status = 'pending') AS pending
5FROM orders;
```

Compute standard deviation and variance.

```
1SELECT
2    stddev(score) AS score_stddev,
3    variance(score) AS score_variance
4FROM test_results;
```

Find median and quartiles.

```
1SELECT
2    percentile_cont(0.25) WITHIN GROUP (ORDER BY salary) AS q1,
3    percentile_cont(0.5) WITHIN GROUP (ORDER BY salary) AS median,
4    percentile_cont(0.75) WITHIN GROUP (ORDER BY salary) AS q3
5FROM employees;
```

- GROUP BY Clause

- Window Functions



---

# Window Functions | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/window.html_

## Window Functions and Queries

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Window functions provide the ability to perform calculations across sets of rows that are related to the current query row. This section introduces window queries and a reference for the window functions supported in Data 360 SQL.

A window function does a calculation across a set of table rows that are related to the current row. Rows in window function calculation remain distinct in the output.

Here's an example that shows how to compare each employee's salary with the average salary in their department:

```
1SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;
```

Result:

```
1depname   | empno | salary |          avg
2----------+-------+--------+-----------------------
3develop   |    11 |   5200 | 5020.0000000000000000
4develop   |     7 |   4200 | 5020.0000000000000000
5develop   |     9 |   4500 | 5020.0000000000000000
6develop   |     8 |   6000 | 5020.0000000000000000
7develop   |    10 |   5200 | 5020.0000000000000000
8personnel |     5 |   3500 | 3700.0000000000000000
9personnel |     2 |   3900 | 3700.0000000000000000
10sales     |     3 |   4800 | 4866.6666666666666667
11sales     |     1 |   5000 | 4866.6666666666666667
12sales     |     4 |   4800 | 4866.6666666666666667
13(10 rows)
```

The first three output columns come directly from the table empsalary, and there's one output row for each row in the table. The fourth column represents an average taken across all the table rows that have the same depname value as the current row.

```
empsalary
```

```
depname
```

A window function call always contains an OVER clause directly after the window function's name and arguments. The OVER clause defines how the rows of the query are split up for processing by the window function. The PARTITION BY clause within OVER divides the rows into groups that share the same values of the PARTITION BY expressions. For each row, the window function is computed across ‌rows that fall into the same partition as the current row.

```
OVER
```

```
PARTITION BY
```

```
OVER
```

```
PARTITION BY
```

You can also control the order in which rows are processed by window functions using ORDER BY within OVER. Here's an example:

```
ORDER BY
```

```
OVER
```

```
1SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC)
2FROM empsalary;
```

Result:

```
1depname   | empno | salary | rank
2----------+-------+--------+-----
3develop   |     8 |   6000 |    1
4develop   |    10 |   5200 |    2
5develop   |    11 |   5200 |    2
6develop   |     9 |   4500 |    4
7develop   |     7 |   4200 |    5
8personnel |     2 |   3900 |    1
9personnel |     5 |   3500 |    2
10sales     |     1 |   5000 |    1
11sales     |     4 |   4800 |    2
12sales     |     3 |   4800 |    2
13(10 rows)
```

In this example, the rank function produces a numerical rank for each unique ORDER BY value in the current row's partition, using the order defined by the ORDER BY clause.The OVER clause defines the behavior of rank.

```
rank
```

```
ORDER BY
```

```
OVER
```

```
rank
```

You can specify ORDER BY independently top-level query ORDER BY output.

```
ORDER BY
```

The query's FROM clause filtered by its WHERE, GROUP BY, and HAVING clauses determine the rows computed in the window function. For example, a row removed because it doesn't meet the WHERE condition isn't seen by any window functions. A query can have many window functions that divide the data in different ways using different OVER clauses. But they all work on the same group of rows defined by the virtual table.

```
FROM
```

```
WHERE
```

```
GROUP BY
```

```
HAVING
```

```
WHERE
```

It's also possible to omit PARTITION BY, in which case there's a single partition containing all rows.

```
PARTITION BY
```

A window frame is a set of rows within the defined partition. Some window functions act only on the rows of the window frame, rather than on the whole partition. If ORDER BY is given, the frame consists of all rows from the start of the partition up to the current row. It also includes any rows that are equal to the current row. If you don’t include ORDER BY, the default frame includes of all rows in the partition. Here's an example using sum:

```
ORDER BY
```

```
sum
```

```
1SELECT salary, sum(salary) OVER () FROM empsalary;
```

Result:

```
1salary |  sum
2-------+-------
3  5200 | 47100
4  5000 | 47100
5  3500 | 47100
6  4800 | 47100
7  3900 | 47100
8  4200 | 47100
9  4500 | 47100
10  4800 | 47100
11  6000 | 47100
12  5200 | 47100
13(10 rows)
```

The SELECT statement includes all rows in the table because there is no ORDER BY or PARTITION BY clauses. Each sum is taken over the whole table, and so we get the same result for each output row. But if we add an ORDER BY clause, we get different results:

```
SELECT
```

```
ORDER BY or PARTITION BY
```

```
ORDER BY
```

```
1SELECT salary, sum(salary) OVER (ORDER BY salary) FROM empsalary;
```

Result:

```
1salary |  sum
2-------+-------
3  3500 |  3500
4  3900 |  7400
5  4200 | 11600
6  4500 | 16100
7  4800 | 25700
8  4800 | 25700
9  5000 | 30700
10  5200 | 41100
11  5200 | 41100
12  6000 | 47100
13(10 rows)
```

Here the sum is taken from the first (lowest) salary up through the current one, including any duplicates of the current one.

You can only include window functions in the SELECT list and the ORDER BY clause of the query. Also, window functions execute after non-window aggregate functions. This means you can include an aggregate function call in the arguments of a window function. But you can’t include a window function in an aggregate function call.

```
SELECT
```

```
ORDER BY
```

To filter or group rows after the window calculations are performed, you can use a sub-select. For example:

```
1SELECT depname, empno, salary, enroll_date
2FROM
3   (SELECT depname, empno, salary, enroll_date,
4   rank() OVER (PARTITION BY depname ORDER BY salary DESC, empno) AS pos
5   FROM empsalary
6   ) AS ss
7WHERE pos < 3;
```

The preceding query only shows the rows from the inner query having rank of less than three.

```
rank
```

If a query has many window functions, name each windowing behavior in a WINDOW clause and then reference it in OVER. For example:

```
WINDOW
```

```
OVER
```

```
1SELECT sum(salary) OVER w, avg(salary) OVER w
2FROM empsalary
3WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);
```

For more details about WINDOW clauses, see SELECT.

```
WINDOW
```

The built-in window functions are listed in the next table. Invoke these functions using window function syntax.

Function

Return Type

Description

row_number()

```
row_number()
```

bigint

```
bigint
```

number of the current row within its partition, counting from 1

rank()

```
rank()
```

bigint

```
bigint
```

rank of the current row with gaps; same as row_number of its first peer

```
row_number
```

modified_rank()

```
modified_rank()
```

bigint

```
bigint
```

rank of the current row with gaps, but taking the lowest rank in case of ties; same as row_number of its last peer

```
row_number
```

dense_rank()

```
dense_rank()
```

bigint

```
bigint
```

rank of the current row without gaps; this function counts peer groups

percent_rank()

```
percent_rank()
```

double precision

```
double precision
```

relative rank of the current row: (rank - 1) / (total partition rows - 1)

```
rank
```

cume_dist()

```
cume_dist()
```

double precision

```
double precision
```

cumulative distribution: (number of partition rows preceding or peer with current row) / total partition rows

ntile(num_buckets integer)

```
ntile(num_buckets integer)
```

integer

```
integer
```

integer ranging from 1 to the argument value, dividing the partition as equally as possible

lag(value anyelement [, offset integer [, default anyelement ]])

```
lag(value anyelement [, offset integer [, default anyelement ]])
```

same type as value

```
same type as value
```

returns <value> evaluated at the row that is <offset> rows before the current row within the partition; if there is no such row, instead return <default> (which must be of the same type as <value>). Both <offset> and <default> are evaluated with respect to the current row. If omitted, <offset> defaults to 1 and <default> to null

```
<value>
```

```
<offset>
```

```
<default>
```

```
<value>
```

```
<offset>
```

```
<default>
```

```
<offset>
```

```
<default>
```

lead(value anyelement [, offset integer [, default anyelement ]])

```
lead(value anyelement [, offset integer [, default anyelement ]])
```

same type as value

```
same type as value
```

returns <value> evaluated at the row that is <offset> rows after the current row within the partition; if there is no such row, instead return <default> (which must be of the same type as <value>). Both <offset> and <default> are evaluated with respect to the current row. If omitted, <offset> defaults to 1 and <default> to null

```
<value>
```

```
<offset>
```

```
<default>
```

```
<value>
```

```
<offset>
```

```
<default>
```

```
<offset>
```

```
<default>
```

first_value(value any)

```
first_value(value any)
```

same type as value

```
same type as value
```

returns <value> evaluated at the row that is the first row of the window frame

```
<value>
```

last_value(value any)

```
last_value(value any)
```

same type as value

```
same type as value
```

returns <value> evaluated at the row that is the last row of the window frame

```
<value>
```

nth_value(value any, nth integer)

```
nth_value(value any, nth integer)
```

same type as value

```
same type as value
```

returns <value> evaluated at the row that is the <nth> row of the window frame (counting from either the first or the last row in the frame, depending on the FROM option); null if no such row

```
<value>
```

```
<nth>
```

```
FROM
```

You can also use most aggregate functions in a window function. For a list of built-in aggregate functions, see Aggregate Functions. Aggregate functions act as window functions if an OVER clause follows the function call.

```
OVER
```

All of the functions depend on the sort ordering specified by the ORDER BY clause of the window definition. Rows that aren't unique in the ORDER BY columns are peers. The four ranking functions (including cume_dist) are defined to give the same answer for all peer rows.

```
ORDER BY
```

```
cume_dist
```

The modified_rank function differs from rank in that it assigns the lowest rank of all entries in case of a tie. For example:

```
modified_rank
```

```
rank
```

```
1SELECT depname, empno, salary,
2   rank() OVER (PARTITION BY depname ORDER BY salary DESC),
3   modified_rank() OVER (PARTITION BY depname ORDER BY salary DESC)
4FROM empsalary;
```

Result:

```
1depname   | empno | salary | rank | modified_rank
2----------+-------+--------+------+--------------
3develop   |     8 |   6000 |    1 |             1
4develop   |    10 |   5200 |    2 |             3
5develop   |    11 |   5200 |    2 |             3
6develop   |     9 |   4500 |    4 |             4
7develop   |     7 |   4200 |    5 |             5
8personnel |     2 |   3900 |    1 |             1
9personnel |     5 |   3500 |    2 |             2
10sales     |     1 |   5000 |    1 |             1
11sales     |     4 |   4800 |    2 |             3
12sales     |     3 |   4800 |    2 |             3
13(10 rows)
```

The first_value, last_value, and nth_value consider only the rows within the window frame. By default, this contains the rows from the start of the partition through the last peer of the current row. You can redefine the frame by adding a suitable frame specification (RANGE, ROWS) to the OVER clause. For information about frame specifications, see Window Function Call Syntax.

```
first_value
```

```
last_value
```

```
nth_value
```

```
RANGE
```

```
ROWS
```

```
OVER
```

If you use an aggregate function as a window function, it aggregates over the rows within the current row's window frame. An aggregate used with ORDER BY and the default window frame definition produces a running sum type of behavior. To obtain aggregation over the whole partition, exclude ORDER BY or use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.

```
ORDER BY
```

```
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
```

cume_dist computes the fraction of partition rows that are less than or equal to the current row and its peers. percent_rank computes the fraction of partition rows that are less than the current row, assuming the current row doesn't exist in the partition.

```
cume_dist
```

```
percent_rank
```

Possible window function call syntax:

```
1<function_name>([ <expression> [, ...] ])
2    [ FROM { FIRST | LAST } ]
3    [ { RESPECT | IGNORE } NULLS ]
4    OVER { ( <window_name> ) | <window_definition> }
5<function_name>(*)
6    [ { RESPECT | IGNORE } NULLS ]
7    OVER { ( <window_name> ) | <window_definition> }
```

where <window_definition> has the syntax:

```
<window_definition>
```

```
1[ <window_name> ]
2[ PARTITION BY <expression> [, ...] ]
3[ ORDER BY <expression> [ ASC | DESC | USING <operator> ] [ NULLS { FIRST | LAST } ] [, ...] ]
4[ <frame_clause> ]
```

The optional <frame_clause> can be one of:

```
<frame_clause>
```

```
1{ RANGE | ROWS } <frame_start> [ <frame_exclusion> ]
2{ RANGE | ROWS } BETWEEN <frame_start> AND <frame_end> [ <frame_exclusion> ]
```

where <frame_start> and <frame_end> can be one of:

```
<frame_start>
```

```
<frame_end>
```

```
1UNBOUNDED PRECEDING
2<offset> PRECEDING
3CURRENT ROW
4<offset> FOLLOWING
5UNBOUNDED FOLLOWING
```

and <frame_exclusion> can be one of:

```
<frame_exclusion>
```

```
1EXCLUDE CURRENT ROW
2EXCLUDE GROUP
3EXCLUDE TIES
4EXCLUDE NO OTHERS
```

In the previous definitions, <expression> represents any value expression that doesn't contain window function calls.

```
<expression>
```

<window_name> is a reference to a named window specification defined in the query's WINDOW clause. Or you can give a full <window_definition> in parentheses. See SELECT. OVER wname isn't equivalent to OVER (wname ...). The latter copies and modifies the window definition, and is rejected if the referenced window specification includes a frame clause.

```
<window_name>
```

```
WINDOW
```

```
<window_definition>
```

```
OVER wname
```

```
OVER (wname ...)
```

The PARTITION BY clause groups the rows of the query into partitions, which are processed separately by the window function. Expressions in the PARTITION BY or ORDER BY clauses can't be output-column names or numbers. Without PARTITION BY, all rows produced by the query are treated as a single partition. The ORDER BY clause determines the order in which the rows of a partition are processed by the window function. Without ORDER BY, rows are processed in an unspecified order.

```
PARTITION BY
```

```
ORDER BY
```

```
PARTITION BY
```

```
ORDER BY
```

The <frame_clause> specifies the set of rows in the window frame, which is a subset of the current partition, for those window functions that act on the frame instead of the whole partition. The set of rows in the frame can vary depending on which row is the current row. The frame can be in RANGE, ROWS or GROUPS mode. The frame runs from the <frame_start> to the <frame_end>. If <frame_end> is omitted, the end defaults to CURRENT ROW.

```
<frame_clause>
```

```
RANGE
```

```
ROWS
```

```
GROUPS
```

```
<frame_start>
```

```
<frame_end>
```

```
CURRENT ROW
```

A <frame_start> of UNBOUNDED PRECEDING means that the frame starts with the first row of the partition, and a <frame_end> of UNBOUNDED FOLLOWING means that the frame ends with the last row of the partition.

```
<frame_start>
```

```
UNBOUNDED PRECEDING
```

```
<frame_end>
```

```
UNBOUNDED FOLLOWING
```

In RANGE or GROUPS mode, a <frame_start> of CURRENT ROW means the frame starts with the current row's first peer row (a row that the window's ORDER BY clause sorts as equivalent to the current row), while a <frame_end> of CURRENT ROW means the frame ends with the current row's last peer row. In ROWS mode, CURRENT ROW is the current row.

```
RANGE
```

```
GROUPS
```

```
<frame_start>
```

```
CURRENT ROW
```

```
ORDER BY
```

```
<frame_end>
```

```
CURRENT ROW
```

```
ROWS
```

```
CURRENT ROW
```

In the <offset> PRECEDING and <offset> FOLLOWING frame options, the <offset> is an expression without variables, aggregate functions, or window functions. The meaning of the <offset> depends on the frame mode:

```
<offset> PRECEDING
```

```
<offset> FOLLOWING
```

```
<offset>
```

- In ROWS mode, the <offset> returns a non-null, non-negative integer, and the option means that the frame starts or ends the specified number of rows before or after the current row.

In ROWS mode, the <offset> returns a non-null, non-negative integer, and the option means that the frame starts or ends the specified number of rows before or after the current row.

```
ROWS
```

```
<offset>
```

- In RANGE mode, the ORDER BY clause specifies exactly one column. The <offset> specifies the maximum difference between the value of that column in the current row and its value in preceding or following rows of the frame. The data type of the <offset> expression varies depending on the data type of the ordering column. For numeric ordering columns, it's the same type as the ordering column. For datetime ordering columns it is an interval. For example, if the ordering column is of type date or timestamp, write RANGE BETWEEN '1 day' PRECEDING AND '10 days' FOLLOWING. The <offset> is still required to be non-null and non-negative, but "non-negative" depends on its data type.

In RANGE mode, the ORDER BY clause specifies exactly one column. The <offset> specifies the maximum difference between the value of that column in the current row and its value in preceding or following rows of the frame. The data type of the <offset> expression varies depending on the data type of the ordering column. For numeric ordering columns, it's the same type as the ordering column. For datetime ordering columns it is an interval. For example, if the ordering column is of type date or timestamp, write RANGE BETWEEN '1 day' PRECEDING AND '10 days' FOLLOWING. The <offset> is still required to be non-null and non-negative, but "non-negative" depends on its data type.

```
RANGE
```

```
ORDER BY
```

```
<offset>
```

```
interval
```

```
date
```

```
timestamp
```

```
RANGE BETWEEN '1 day' PRECEDING AND '10 days' FOLLOWING
```

```
<offset>
```

In any case, the distance to the end of the frame is limited by the distance to the end of the partition. Rows near the end of the partition can contain fewer rows.

Notice that in ROWS mode, 0 PRECEDING and 0 FOLLOWING is equivalent to CURRENT ROW. This normally holds in RANGE mode as well, for an appropriate data-type-specific meaning of zero.

```
ROWS
```

```
0 PRECEDING
```

```
0 FOLLOWING
```

```
CURRENT ROW
```

```
RANGE
```

The <frame_exclusion> option allows rows around the current row to be excluded from the frame. EXCLUDE CURRENT ROW excludes the current row from the frame. EXCLUDE GROUP excludes the current row and its ordering peers from the frame. EXCLUDE TIES excludes any peers of the current row from the frame, but not the current row itself. EXCLUDE NO OTHERS defines the default behavior of not excluding the current row or its peers.

```
<frame_exclusion>
```

```
EXCLUDE CURRENT ROW
```

```
EXCLUDE GROUP
```

```
EXCLUDE TIES
```

```
EXCLUDE NO OTHERS
```

The default framing option is RANGE UNBOUNDED PRECEDING, which is the same as RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. With ORDER BY, this sets the frame to be all rows from the partition start through the current row's last ORDER BY peer. Without ORDER BY, this means all rows of the partition are included in the window frame.

```
RANGE UNBOUNDED PRECEDING
```

```
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
```

```
ORDER BY
```

<frame_start> can’t be UNBOUNDED FOLLOWING, <frame_end> can’t be UNBOUNDED PRECEDING, and the <frame_end> can’t be earlier in the list of <frame_start> and <frame_end> options than the <frame_start> is. For example RANGE BETWEEN CURRENT ROW AND offset PRECEDING isn’t allowed. But, ROWS BETWEEN 7 PRECEDING AND 8 PRECEDING is allowed, even though it never selects any rows.

```
<frame_start>
```

```
UNBOUNDED FOLLOWING
```

```
<frame_end>
```

```
UNBOUNDED PRECEDING
```

```
<frame_end>
```

```
<frame_start>
```

```
<frame_end>
```

```
<frame_start>
```

```
RANGE BETWEEN CURRENT ROW AND offset PRECEDING
```

```
ROWS BETWEEN 7 PRECEDING AND 8 PRECEDING
```

The FROM FIRST or FROM LAST options are only valid for the nth_value function. They specify whether the n-th value is counted from the first row or the last row in the frame. The default is FROM FIRST.

```
FROM FIRST
```

```
FROM LAST
```

```
nth_value
```

```
FROM FIRST
```

For some window functions, NULL values within a window frame can cause undesired results. A common example is a table imported from a spreadsheet where values aren't repeated for rows of the same group. For example:

```
NULL
```

```
1row_no | country | region | amount
2 -------+---------+--------+-------
3      1 | USA     | North  |   1000
4      2 | NULL    | East   |   1200
5      3 | NULL    | West   |   3000
6      4 | NULL    | South  |   2600
7      5 | Germany | North  |   1800
8      6 | NULL    | East   |   2700
9      7 | NULL    | West   |   1100
10      8 | NULL    | South  |   2100
```

In this example, the country value only occurs in the first value of each group, but a query with a filter by region can return all rows with NULL in the country column. You can fix this with the IGNORE NULLS clause and the last_value function:

```
country
```

```
region
```

```
NULL
```

```
country
```

```
IGNORE NULLS
```

```
last_value
```

```
1SELECT last_value(country) IGNORE NULLS OVER (ORDER BY row_no)
2   region,
3   amount
4FROM regions
```

This query produces the result:

```
1country | region | amount
2--------+--------+-------
3USA     | North  |   1000
4USA     | East   |   1200
5USA     | West   |   3000
6USA     | South  |   2600
7Germany | North  |   1800
8Germany | East   |   2700
9Germany | West   |   1100
10Germany | South  |   2100
```

IGNORE NULLS is only supported for the last_value function. The clause RESPECT NULLS doesn't ignore NULL values and is the default behavior.

```
IGNORE NULLS
```

```
last_value
```

```
RESPECT NULLS
```

```
NULL
```

In Data 360 SQL the IGNORE NULLS option is only supported for the last_value function.

```
IGNORE NULLS
```

```
last_value
```

Note

To caing parameter-less aggregate functions as window functions, use syntaxes with *. For example count(*) OVER (PARTITION BY x ORDER BY y).Window-specific functions don’t support DISTINCT or ORDER BY within the function argument list.

```
*.
```

```
count(*) OVER (PARTITION BY x ORDER BY y)
```

```
DISTINCT
```

```
ORDER BY
```

Window function calls can only be in the SELECT list and the ORDER BY clause of the query.

```
SELECT
```

```
ORDER BY
```



---

# Set Returning Functions | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/set-returning-functions.html_

## Set Returning Functions

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

This section describes functions that return multiple rows. Set returning functions can be called in the FROM clause of a SQL query:

```
1SELECT * FROM unnest(ARRAY['Mon','Tue','Wed','Thu','Fri']) AS days(day)
```

- unnest - Expands the elements of an array.

```
unnest
```

- external - Reads data stored in an external file format from one or multiple external locations.

```
external
```

- generate_series (Numerical) - Generates a series of numerical values from start to stop with a given step size.

```
generate_series
```

- generate_series (Time-based) - Generates a series of date or timestamp values from start to stop with a given interval step size.

```
generate_series
```

In SQL, all row sets are unordered by default. Due to implementation details such as parallelization, rows might be processed and output in any order.

Sometimes, the order of rows generated by a set returning function is important, though. In such cases, the WITH ORDINALITY clause can be used to add an additional output column, assigning a sequential number to the generated rows:

```
WITH ORDINALITY
```

```
1SELECT *
2FROM unnest(ARRAY['Mon','Tue','Wed','Thu','Fri']) WITH ORDINALITY AS days(day, ordinality)
3ORDER BY ordinality
```

Results:

day

ordinality

Mon

1

Tue

2

Wed

3

Thu

4

Fri

5

The WITH ORDINALITY clause is supported by most, but not all, set returning functions.

```
WITH ORDINALITY
```

- SELECT

- FROM Clause



---

# SQL Commands for Tableau Hyper API | Data 360 SQL Reference | Data 360 Query Guide

_URL: https://developer.salesforce.com/docs/data/data-cloud-query-guide/references/dc-sql-reference/tableau-sql-commands-context.html_

## SQL Commands for Tableau Hyper API

Applies to: ❌ Data 360 SQL ✅ Tableau Hyper API

Check out these SQL commands that perform write operations.

For more information about write operations in Tableau Hyper API, see Execute SQL Commands.

The Hyper API SQL commands can be grouped into these categories:

- Data modification commands: Using INSERT, UPDATE, DELETE, and TRUNCATE, one can change the data stored inside a table. Furthermore, COPY FROM copies data directly from an external file (e.g., a CSV or Parquet file) into a table. When combined with SELECT queries, one can also use those commands to, e.g., materialize the results of complex analytical computations.

- Data definition commands: Data Cloud SQL supports commands to create databases, tables and modify ("alter") and delete ("drop") them.

- Support statements for prepared statements: PREPARE can pre-compile a query, including placeholder parameters. A prepared query can later on be executed through EXECUTE, giving specific values for the parameters. DEALLOCATE deallocates a given query.