-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcrud.sql
More file actions
170 lines (142 loc) · 3.34 KB
/
crud.sql
File metadata and controls
170 lines (142 loc) · 3.34 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---- db: -h localhost -p 7890 -U postgres postgres
-- Read
-- equivalent to #> operator
select jsonb_extract_path(
'{"attr": "value", "nested": {"foo": "bar"}}'::jsonb,
'nested', 'foo'
);
-- equivalent to #>> operator
select jsonb_extract_path_text(
'{"attr": "value", "nested": {"foo": "bar"}}'::jsonb,
'nested', 'foo'
);
----
-- Cast from string
select '{"gender": "male", "birthDate": "1990.11.07" }'::jsonb;
---- JSONB constructors
----
-- json_build_object
select jsonb_pretty(
jsonb_build_object(
'gender', 'female',
'deceased', false,
'growth', 180,
'birthDate', '1982.10.12',
'foo', '{"mar": "tar"}'::jsonb
));
----
-- json_build_array
select jsonb_pretty(
jsonb_build_array('one', 'two', 3, false, null)
);
---- Compose
\a
select jsonb_pretty(
jsonb_build_object(
'gender', 'male',
'deceased', false,
'growth', 180,
'birthDate', '1982.10.12',
'address', '[{"city": "SPB"}]'::jsonb,
'name', jsonb_build_array(jsonb_build_object('family', 'Petrov'))
));
----
\a
-- jsonb_strip_nulls
select jsonb_pretty(
jsonb_strip_nulls(
jsonb_build_object(
'gender', null,
'birthDate', '1982.10.12',
'address', '[{"city": null}]'::jsonb
)));
----
-- row_to_jsonb
-- ~ 330 columns in usnpi table
\a
\d+ usnpi
----
\a
\timing
select jsonb_pretty(
jsonb_strip_nulls(
row_to_json(u.*)::jsonb
)
)
from usnpi u
limit 1;
----
-- Update
-- || - set insert
----
select '["a", "b"]'::jsonb || '["c", "d"]'::jsonb;
----
select '{"a": "b"}'::jsonb || '{"c": "d"}'::jsonb;
----
select '{"a": "b"}'::jsonb || '["c", "d"]'::jsonb;
----
select '["a", "b"]'::jsonb || '{"c": "d"}'::jsonb;
----
select '["a", "b"]'::jsonb || '"c"'::jsonb;
----
select '{"a": "b"}'::jsonb || '"c"'::jsonb;
----
-- single
select '{"a": "b", "nested": {"attr": "val"}}'::jsonb - 'a';
----
-- multiple
select '{"a": "b", "nested": {"attr": "val", "b": "c"}}'::jsonb - '{nested, a}'::text[];
----
-- by path
select '{"a": "b", "nested": {"attr": "val", "b": "c"}}'::jsonb #- '{nested,attr}'::text[];
----
\a
-- set
select jsonb_pretty(
jsonb_set('{"a": "b"}', '{c}', '"d"')
);
select jsonb_pretty(
jsonb_set('{"a": "b", "nested": {"c": "foo"}}', '{nested,c}', '"bar"')
);
----
-- insert
\a
select jsonb_pretty(
jsonb_insert('{"a": "b"}', '{c}', '"d"')
);
--- return error!
select jsonb_pretty(
jsonb_insert('{"a": "b", "nested": {"c": "foo"}}', '{nested,c}', '"bar"')
);
----
----
-- Examples
-- add condition code
----
\a
select jsonb_pretty(resource)
from condition
limit 5;
----
update condition
set resource = jsonb_set(resource, '{code,coding}',
(resource#>'{code, coding}' ||
'[{"code": "J32.9",
"system": "https://icd10",
"display": "Sinusitis (chronic) NOS"}]'))
-- where Chronic sinusitis
where resource#>'{code,coding}' @> '[{"code": "40055000", "system": "http://snomed.info/sct"}]'
and not (resource#>'{code,coding}' @> '[{"code": "J32.9", "system": "https://icd10"}]')
----
select jsonb_pretty(resource#>'{code, coding}')
from condition
where resource#>'{code,coding}' @> '[{"code": "40055000", "system": "http://snomed.info/sct"}]'
limit 10;
----
----
--truncate usnpi;
--\copy usnpi FROM '/tmp/npi.csv' with null '' CSV HEADER;
--select healthcare_provider_taxonomy_group_15 from usnpi
--where healthcare_provider_taxonomy_group_15 is null limit 10;
--select healthcare_provider_taxonomy_group_15 from usnpi limit 10;
----