You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### ARRAY JOIN and LEFT ARRAY JOIN {#array-join-left-array-join-examples}
32
+
31
33
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:
32
34
33
35
```sql
@@ -86,6 +88,80 @@ LEFT ARRAY JOIN arr;
86
88
└─────────────┴─────┘
87
89
```
88
90
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
+
FROMtest.hits
100
+
ARRAY JOIN
101
+
GoalsReached,
102
+
arrayEnumerate(GoalsReached) AS num
103
+
WHERE CounterID =160656
104
+
LIMIT10
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
+
FROMtest.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.IDAS GoalID,
138
+
sum(Sign) AS Reaches,
139
+
sumIf(Sign, num =1) AS Visits
140
+
FROMtest.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
+
LIMIT10
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
+
89
165
## Using Aliases {#using-aliases}
90
166
91
167
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