@@ -17,6 +17,8 @@ into formulas that involve simpler measures. Also, calculated measures [can
1717help] [ ref-decomposition-recipe ] to use [ non-additive] [ ref-non-additive ] measures with
1818pre-aggregations.
1919
20+ ### Members of the same cube
21+
2022In the following example, the ` completed_ratio ` measure is calculated as a division of
2123` completed_count ` by total ` count ` . Note that the result is also multiplied by ` 1.0 `
2224since [ integer division in SQL] [ link-postgres-division ] would otherwise produce an
@@ -90,6 +92,149 @@ FROM (
9092) AS " orders"
9193```
9294
95+ ### Members of other cubes
96+
97+ If you have ` first_cube ` that is [ joined] [ ref-joins ] to ` second_cube ` , you can define a
98+ calculated measure that references measures from both ` first_cube ` and ` second_cube ` .
99+ When you query for this calculated measure, Cube will transparently generate SQL with
100+ necessary joins.
101+
102+ In the following example, the ` orders.purchases_to_users_ratio ` measure references the
103+ ` purchases ` measure from the ` orders ` cube and the ` count ` measure from the ` users ` cube:
104+
105+ <CodeTabs >
106+
107+ ``` javascript
108+ cube (` orders` , {
109+ sql: `
110+ SELECT 1 AS id, 11 AS user_id, 'processing' AS status UNION ALL
111+ SELECT 2 AS id, 11 AS user_id, 'completed' AS status UNION ALL
112+ SELECT 3 AS id, 11 AS user_id, 'completed' AS status
113+ ` ,
114+
115+ dimensions: {
116+ id: {
117+ sql: ` id` ,
118+ type: ` number` ,
119+ primary_key: true
120+ }
121+ },
122+
123+ measures: {
124+ purchases: {
125+ type: ` count` ,
126+ filters: [{
127+ sql: ` ${ CUBE } .status = 'completed'`
128+ }]
129+ }
130+ }
131+ })
132+
133+ cube (` users` , {
134+ sql: `
135+ SELECT 11 AS id, 'Alice' AS name UNION ALL
136+ SELECT 12 AS id, 'Bob' AS name UNION ALL
137+ SELECT 13 AS id, 'Eve' AS name
138+ ` ,
139+
140+ joins: {
141+ orders: {
142+ sql: ` ${ CUBE } .id = ${ orders} .user_id` ,
143+ relationship: ` one_to_many`
144+ }
145+ },
146+
147+ dimensions: {
148+ id: {
149+ sql: ` id` ,
150+ type: ` number` ,
151+ primary_key: true
152+ }
153+ },
154+
155+ measures: {
156+ count: {
157+ type: ` count`
158+ },
159+
160+ purchases_to_users_ratio: {
161+ sql: ` 100.0 * ${ orders .purchases } / ${ CUBE .count } ` ,
162+ type: ` number` ,
163+ format: ` percent`
164+ }
165+ }
166+ })
167+ ```
168+
169+ ``` yaml
170+ cubes :
171+ - name : orders
172+ sql : >
173+ SELECT 1 AS id, 11 AS user_id, 'processing' AS status UNION ALL
174+ SELECT 2 AS id, 11 AS user_id, 'completed' AS status UNION ALL
175+ SELECT 3 AS id, 11 AS user_id, 'completed' AS status
176+
177+ dimensions :
178+ - name : id
179+ sql : id
180+ type : number
181+ primary_key : true
182+
183+ measures :
184+ - name : purchases
185+ type : count
186+ filters :
187+ - sql : " {CUBE}.status = 'completed'"
188+
189+ - name : users
190+ sql : >
191+ SELECT 11 AS id, 'Alice' AS name UNION ALL
192+ SELECT 12 AS id, 'Bob' AS name UNION ALL
193+ SELECT 13 AS id, 'Eve' AS name
194+
195+ joins :
196+ - name : orders
197+ sql : " {CUBE}.id = {orders}.user_id"
198+ relationship : one_to_many
199+
200+ dimensions :
201+ - name : id
202+ sql : id
203+ type : number
204+ primary_key : true
205+
206+ measures :
207+ - name : count
208+ type : count
209+
210+ - name : purchases_to_users_ratio
211+ sql : " 1.0 * {orders.purchases} / {CUBE.count}"
212+ type : number
213+ ` ` `
214+
215+ </CodeTabs>
216+
217+ If you query for ` users.purchases_to_users_ratio`, Cube will generate the following SQL:
218+
219+ ` ` ` sql
220+ SELECT
221+ 1.0 * COUNT(
222+ CASE
223+ WHEN ("orders".status = 'completed') THEN "orders".id
224+ END
225+ ) / COUNT(DISTINCT "users".id) "users__purchases_to_users_ratio"
226+ FROM (
227+ SELECT 11 AS id, 'Alice' AS name UNION ALL
228+ SELECT 12 AS id, 'Bob' AS name UNION ALL
229+ SELECT 13 AS id, 'Eve' AS name
230+ ) AS "users"
231+ LEFT JOIN (
232+ SELECT 1 AS id, 11 AS user_id, 'processing' AS status UNION ALL
233+ SELECT 2 AS id, 11 AS user_id, 'completed' AS status UNION ALL
234+ SELECT 3 AS id, 11 AS user_id, 'completed' AS status
235+ ) AS "orders" ON "users".id = "orders".user_id
236+ ` ` `
237+
93238# # Proxy dimensions
94239
95240**Proxy dimensions reference dimensions from the same cube or other cubes.**
0 commit comments