forked from itversity/databricks-sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
06 Queries to Process Values in JSON String Columns.sql
85 lines (64 loc) · 2.16 KB
/
06 Queries to Process Values in JSON String Columns.sql
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
SELECT * FROM lms_bronze.course_catalog;
SELECT DISTINCT table_name
FROM lms_bronze.course_catalog;
SELECT table_name, COUNT(*) AS record_count
FROM lms_bronze.course_catalog
GROUP BY 1;
DESCRIBE lms_bronze.course_catalog;
SELECT *
FROM lms_bronze.course_catalog
WHERE table_name = 'courses';
SELECT table_name, record
FROM lms_bronze.course_catalog
WHERE table_name = 'courses';
SELECT current_timestamp();
SELECT current_database();
SELECT current_date();
SELECT date_format('2022-08-15', 'yyyy-MM');
SELECT date_format(current_date(), 'yyyy-MM');
SELECT table_name, record FROM lms_bronze.course_catalog;
WITH icte AS
(
SELECT from_json(
'{"instructor_id": 1, "instructor_name": "ITVersity, LLC"}',
'struct<instructor_id:INT, instructor_name:STRING>'
) AS record
)
SELECT record.* FROM icte;
WITH ccte AS
(
SELECT from_json(
'{"course_id": 1, "instructor_id": 1, "course_title": "Mastering Python"}',
'struct<course_id:INT, instructor_id:INT, course_title:STRING>'
) AS record
)
SELECT record.* FROM ccte;
WITH instructors AS (
SELECT table_name,
from_json(record, 'struct<instructor_id:INT, instructor_name:STRING>') AS record
FROM lms_bronze.course_catalog
WHERE table_name = 'instructors'
) SELECT table_name, record.* FROM instructors;
WITH courses AS (
SELECT table_name,
from_json(record, 'struct<course_id:INT, instructor_id:INT, course_title:STRING>') AS record
FROM lms_bronze.course_catalog
WHERE table_name = 'courses'
) SELECT table_name, record.* FROM courses;
CREATE OR REPLACE VIEW lms_bronze.instructors_v
AS
SELECT table_name,
from_json(record, 'struct<instructor_id:INT, instructor_name:STRING>') AS record
FROM lms_bronze.course_catalog
WHERE table_name = 'instructors';
SELECT table_name, record.* FROM lms_bronze.instructors_v;
CREATE OR REPLACE VIEW lms_bronze.courses_v
AS
SELECT table_name,
from_json(record, 'struct<course_id:INT, instructor_id:INT, course_title:STRING>') AS record
FROM lms_bronze.course_catalog
WHERE table_name = 'courses';
SELECT table_name, record.* FROM lms_bronze.courses_v;
USE lms_bronze;
SHOW tables;
DESCRIBE lms_bronze.instructors_v;