Skip to content

Commit f08977a

Browse files
authored
Update array-join.md with examples from function docs
1 parent 4c7876f commit f08977a

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

docs/en/sql-reference/statements/select/array-join.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Supported types of `ARRAY JOIN` are listed below:
2828

2929
## Basic ARRAY JOIN Examples {#basic-array-join-examples}
3030

31+
### ARRAY JOIN and LEFT ARRAY JOIN {#array-join-left-array-join-examples}
32+
3133
The examples below demonstrate the usage of the `ARRAY JOIN` and `LEFT ARRAY JOIN` clauses. Let's create a table with an [Array](../../../sql-reference/data-types/array.md) type column and insert values into it:
3234

3335
```sql
@@ -86,6 +88,80 @@ LEFT ARRAY JOIN arr;
8688
└─────────────┴─────┘
8789
```
8890

91+
### ARRAY JOIN and arrayEnumerate function {#array-join-arrayEnumerate}
92+
93+
This function is normally used with `ARRAY JOIN`. It allows counting something just once for each array after applying `ARRAY JOIN`. Example:
94+
95+
```sql
96+
SELECT
97+
count() AS Reaches,
98+
countIf(num = 1) AS Hits
99+
FROM test.hits
100+
ARRAY JOIN
101+
GoalsReached,
102+
arrayEnumerate(GoalsReached) AS num
103+
WHERE CounterID = 160656
104+
LIMIT 10
105+
```
106+
107+
```text
108+
┌─Reaches─┬──Hits─┐
109+
│ 95606 │ 31406 │
110+
└─────────┴───────┘
111+
```
112+
113+
In this example, Reaches is the number of conversions (the strings received after applying `ARRAY JOIN`), and Hits is the number of pageviews (strings before `ARRAY JOIN`). In this particular case, you can get the same result in an easier way:
114+
115+
```sql
116+
SELECT
117+
sum(length(GoalsReached)) AS Reaches,
118+
count() AS Hits
119+
FROM test.hits
120+
WHERE (CounterID = 160656) AND notEmpty(GoalsReached)
121+
```
122+
123+
```text
124+
┌─Reaches─┬──Hits─┐
125+
│ 95606 │ 31406 │
126+
└─────────┴───────┘
127+
```
128+
129+
### ARRAY JOIN and arrayEnumerateUniq {#array_join_arrayEnumerateUniq}
130+
131+
This function is useful when using `ARRAY JOIN` and aggregating array elements.
132+
133+
In this example, each goal ID has a calculation of the number of conversions (each element in the Goals nested data structure is a goal that was reached, which we refer to as a conversion) and the number of sessions. Without `ARRAY JOIN`, we would have counted the number of sessions as sum(Sign). But in this particular case, the rows were multiplied by the nested Goals structure, so in order to count each session one time after this, we apply a condition to the value of the `arrayEnumerateUniq(Goals.ID)` function.
134+
135+
```sql
136+
SELECT
137+
Goals.ID AS GoalID,
138+
sum(Sign) AS Reaches,
139+
sumIf(Sign, num = 1) AS Visits
140+
FROM test.visits
141+
ARRAY JOIN
142+
Goals,
143+
arrayEnumerateUniq(Goals.ID) AS num
144+
WHERE CounterID = 160656
145+
GROUP BY GoalID
146+
ORDER BY Reaches DESC
147+
LIMIT 10
148+
```
149+
150+
```text
151+
┌──GoalID─┬─Reaches─┬─Visits─┐
152+
│ 53225 │ 3214 │ 1097 │
153+
│ 2825062 │ 3188 │ 1097 │
154+
│ 56600 │ 2803 │ 488 │
155+
│ 1989037 │ 2401 │ 365 │
156+
│ 2830064 │ 2396 │ 910 │
157+
│ 1113562 │ 2372 │ 373 │
158+
│ 3270895 │ 2262 │ 812 │
159+
│ 1084657 │ 2262 │ 345 │
160+
│ 56599 │ 2260 │ 799 │
161+
│ 3271094 │ 2256 │ 812 │
162+
└─────────┴─────────┴────────┘
163+
```
164+
89165
## Using Aliases {#using-aliases}
90166

91167
An alias can be specified for an array in the `ARRAY JOIN` clause. In this case, an array item can be accessed by this alias, but the array itself is accessed by the original name. Example:

0 commit comments

Comments
 (0)