-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_query.sql
More file actions
28 lines (28 loc) · 1.05 KB
/
Copy pathsql_query.sql
File metadata and controls
28 lines (28 loc) · 1.05 KB
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
SELECT table_2.dimension_1, table_2.dimension_2, SUM(table_1.measure_1) AS measure_1, SUM(table_2.measure_2) AS measure_2
FROM (
SELECT
a.dimension_1,
COALESCE(a.measure_1, 0) AS measure_1,
m.correct_dimension_2 AS dimension_2
FROM table_a AS a
INNER JOIN (
SELECT DISTINCT dimension_1, correct_dimension_2
FROM table_map
WHERE dimension_1 IS NOT NULL AND correct_dimension_2 IS NOT NULL
) AS m ON a.dimension_1 = m.dimension_1
) AS table_1
INNER JOIN (
SELECT
b.dimension_1,
COALESCE(b.measure_2, 0) AS measure_2,
m.correct_dimension_2 AS dimension_2
FROM table_b AS b
INNER JOIN (
SELECT DISTINCT dimension_1, correct_dimension_2
FROM table_map
WHERE dimension_1 IS NOT NULL AND correct_dimension_2 IS NOT NULL
) AS m ON b.dimension_1 = m.dimension_1
) AS table_2
ON table_1.dimension_1 = table_2.dimension_1
AND table_1.dimension_2 = table_2.dimension_2
GROUP BY table_2.dimension_1, table_2.dimension_2;