-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06.json-relational-joins.sql
More file actions
139 lines (110 loc) · 3.95 KB
/
06.json-relational-joins.sql
File metadata and controls
139 lines (110 loc) · 3.95 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
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
-- =====================================================
-- Join JSON Event Data with Customer Table
-- Demonstrates: Extract JSON field to join relational data
-- =====================================================
SELECT c.c_custkey,
c.c_name,
e.event_data ->> 'event_type' AS event_type,
e.created_at
FROM customer_events e
JOIN customer c
ON c.c_custkey = e.customer_id
LIMIT 20;
-- =====================================================
-- Find Customers Who Made Purchases (JSON Filter + Join)
-- Demonstrates: Filtering JSON before joining
-- =====================================================
SELECT c.c_custkey,
c.c_name,
e.event_data ->> 'event_type' AS event_type,
e.event_data ->> 'amount' AS purchase_amount
FROM customer_events e
JOIN customer c
ON c.c_custkey = e.customer_id
WHERE e.event_data ->> 'event_type' = 'purchase'
LIMIT 20;
-- =====================================================
-- Join Orders with JSON Events
-- Demonstrates: Combining structured transactions with event logs
-- =====================================================
SELECT o.o_orderkey,
o.o_totalprice,
e.event_data ->> 'event_type' AS event_type,
e.created_at
FROM orders o
JOIN customer_events e
ON e.customer_id = o.o_custkey
WHERE e.event_data ->> 'event_type' = 'purchase'
LIMIT 20;
-- =====================================================
-- Aggregate JSON Events per Customer
-- Demonstrates: Relational GROUP BY with JSON extraction
-- =====================================================
SELECT c.c_name,
e.event_data ->> 'event_type' AS event_type,
COUNT(*) AS event_count
FROM customer_events e
JOIN customer c
ON c.c_custkey = e.customer_id
GROUP BY c.c_name, event_type
ORDER BY event_count DESC
LIMIT 20;
-- =====================================================
-- Join Nested JSON Fields
-- Demonstrates: Extract nested JSON metadata
-- =====================================================
SELECT c.c_name,
e.event_data -> 'metadata' ->> 'browser' AS browser,
COUNT(*) AS events
FROM customer_events e
JOIN customer c
ON c.c_custkey = e.customer_id
GROUP BY c.c_name, browser
ORDER BY events DESC
LIMIT 20;
-- =====================================================
-- Join JSON Events with LATERAL for Analysis
-- Demonstrates: LATERAL join for per-customer event statistics
-- =====================================================
SELECT c.c_name,
stats.total_events,
stats.total_purchases
FROM customer c
JOIN LATERAL (
SELECT COUNT(*) AS total_events,
COUNT(*) FILTER (
WHERE event_data ->> 'event_type' = 'purchase'
) AS total_purchases
FROM customer_events e
WHERE e.customer_id = c.c_custkey
) stats ON TRUE
LIMIT 20;
-- =====================================================
-- Combine JSON Path Filtering with Relational Join
-- Demonstrates: Advanced JSON filtering in joins
-- =====================================================
SELECT c.c_name,
e.created_at,
e.event_data
FROM customer_events e
JOIN customer c
ON c.c_custkey = e.customer_id
WHERE jsonb_path_exists(
e.event_data,
'$.metadata ? (@.browser == "chrome")'
)
LIMIT 20;
-- =====================================================
-- Customers with High-Value Purchases
-- Demonstrates: JSON numeric extraction with relational joins
-- =====================================================
SELECT c.c_name,
(e.event_data ->> 'amount')::INT AS purchase_amount,
e.created_at
FROM customer_events e
JOIN customer c
ON c.c_custkey = e.customer_id
WHERE e.event_data ->> 'event_type' = 'purchase'
AND (e.event_data ->> 'amount')::INT > 500
ORDER BY purchase_amount DESC
LIMIT 20;