-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinvoice-extract.sql
147 lines (132 loc) · 3.33 KB
/
invoice-extract.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
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
140
141
142
143
144
145
146
147
-- Create a test JSON row in a temporary file.
declare global temporary table JSON_INVOICES as (
select cast(J_DATA as clob(10K)) as J_DATA
from(values(
'{
"invoices": [
{
"invoiceNumber": 2019001,
"customer": 1,
"address": "Somewhere in Wiesbaden, Germany",
"email": "[email protected]",
"lines": [
{
"lineNum": 1,
"item": "SPARE_PART-1",
"quantity": 3,
"price": 9.99,
"discountCodes": [
"XX",
"YY"
]
},
{
"lineNum": 2,
"item": "SPARE_PART-2",
"quantity": 3,
"price": 1.23
}
]
},
{
"invoiceNumber": 2019002,
"customer": 2,
"address": "Somewhere in Guildford, UK",
"lines": [
{
"lineNum": 1,
"item": "SPARE_PART-3",
"quantity": 3,
"price": 23.45
},
{
"lineNum": 2,
"item": "SPARE_PART-4",
"quantity": 3,
"price": 87.43,
"discountCodes": [
"AA",
"YY"
]
}
]
}
]
}')) x(J_DATA)
) with data with replace
;;
-- Extract invoice header data.
declare global temporary table INVOICE_HEADER as (
select J.INVOICE_NUMBER,
J.CUSTOMER,
J.ADDRESS,
J.EMAIL
from QTEMP.JSON_INVOICES JI
cross join json_table(
JI.J_DATA,
'strict $.invoices[*]' -- Extract all elements of "invoices" array
columns(
INVOICE_NUMBER int path 'strict $.invoiceNumber',
CUSTOMER int path 'strict $.customer',
ADDRESS varchar(256) path 'strict $.address',
EMAIL varchar(256) path 'lax $.email' default '' on empty
)
) J
) with data with replace;;
-- Extract invoice line data.
declare global temporary table INVOICE_LINE as (
select J.INVOICE_NUMBER,
J.LINE_NUM,
J.ITEM,
J.QUANTITY,
J.PRICE
from QTEMP.JSON_INVOICES JI
cross join json_table(
JI.J_DATA,
'$.invoices[*]'
columns(
INVOICE_NUMBER integer path '$.invoiceNumber',
nested path '$.lines[*]' -- Extract all "lines" for all "invoices"
columns(
LINE_NUM integer path '$.lineNum',
ITEM varchar(50) path '$.item',
QUANTITY integer path '$.quantity',
PRICE decimal(10, 2) path '$.price'
)
)
) J
) with data with replace
;;
-- Extract invoice discount code data.
declare global temporary table INVOICE_DISCOUNT as (
select J.INVOICE_NUMBER,
J.LINE_NUM,
J.DISCOUNT_CODE
from QTEMP.JSON_INVOICES JI
cross join json_table(
JI.J_DATA,
'$.invoices[*]'
columns(
INVOICE_NUMBER integer path '$.invoiceNumber',
nested path '$.lines[*]'
columns(
LINE_NUM integer path '$.lineNum',
nested path 'lax $.discountCodes[*]' -- Extract all "discounts" for all "lines" for all "invoices"
columns(
DISCOUNT_CODE char(2) path '$'
)
)
)
) J
where DISCOUNT_CODE is not null
) with data with replace
;;
-- Join up the files to show the results...
select *
from QTEMP.INVOICE_HEADER H
join QTEMP.INVOICE_LINE L
on L.INVOICE_NUMBER = H.INVOICE_NUMBER
left join QTEMP.INVOICE_DISCOUNT D
on D.INVOICE_NUMBER = L.INVOICE_NUMBER and
D.LINE_NUM = L.LINE_NUM
;;