]> git.evergreen-ils.org Git - working/Evergreen.git/blob - docs/TechRef/qstore/stored_queries.txt
LP#1678638: technical documentation for qstore
[working/Evergreen.git] / docs / TechRef / qstore / stored_queries.txt
1 Stored Queries
2
3 1: JOIN
4
5 SELECT
6    "aou".id AS "id",
7    "aou".name AS "name",
8    "aou".shortname AS "short_name"
9 FROM
10    actor.org_unit AS "aou"
11       INNER JOIN actor.org_unit_type AS "aout"
12          ON TRUE
13 WHERE
14    "aou".parent_ou > 3;
15
16 ------------------------------------------------------------
17 2: UNION
18
19 SELECT
20    "aou".id AS "id",
21    "aou".name AS "name",
22    "aou".shortname AS "short_name"
23 FROM
24    actor.org_unit AS "aou"
25       INNER JOIN actor.org_unit_type AS "aout"
26          ON TRUE
27 WHERE
28    "aou".parent_ou > 3
29 UNION
30 SELECT
31    "aou".id AS "id",
32    "aou".name AS "name",
33    "aou".shortname AS "short_name"
34 FROM
35    actor.org_unit AS "aou"
36       INNER JOIN actor.org_unit_type AS "aout"
37          ON TRUE
38 WHERE
39    "aou".parent_ou > 3;
40
41 -----------------------------------------------------------
42 3: Simple query (to be used elsewhere as a subquery)
43
44 SELECT
45    "aou".ou_type
46 FROM
47    actor.org_unit AS "aou"
48 WHERE
49    "aou".id = 3;
50
51 -------------------------------------------------------------
52 4: SELECTing a subquery
53
54 SELECT
55    "aout".id,
56    (
57       SELECT
58          "aou".ou_type
59       FROM
60          actor.org_unit AS "aou"
61       WHERE
62          "aou".id = 3
63    )
64 FROM
65    actor.org_unit_type AS "aout";
66
67 --------------------------------------------------------------
68 5: IN clause with a subquery
69
70 SELECT
71    "aout".id
72 FROM
73    actor.org_unit_type AS "aout"
74 WHERE
75    "aout".id IN (
76       SELECT
77          "aou".ou_type
78       FROM
79          actor.org_unit AS "aou"
80       WHERE
81          "aou".id = 3
82    );
83
84 ---------------------------------------------------------------
85 6: Subquery in the FROM clause
86
87 SELECT
88    "aou".ou_type
89 FROM
90    (
91       SELECT
92          "aou".ou_type
93       FROM
94          actor.org_unit AS "aou"
95       WHERE
96          "aou".id = 3
97    ) AS "aou";
98
99 ----------------------------------------------------------------
100 7: JOINing to a subquery
101
102 SELECT
103    "aou".ou_type AS "goober"
104 FROM
105    actor.usr AS "au"
106       INNER JOIN (
107          SELECT
108             "aou".ou_type
109          FROM
110             actor.org_unit AS "aou"
111          WHERE
112             "aou".id = 3
113       ) AS "aou"
114          ON "au".id = "aou".ou_type;
115
116 ----------------------------------------------------------------
117 8: EXISTS clause
118
119 SELECT
120    "aout".id
121 FROM
122    actor.org_unit_type AS "aout"
123 WHERE
124    EXISTS (
125       SELECT
126          "aou".ou_type
127       FROM
128          actor.org_unit AS "aou"
129       WHERE
130          "aou".id = 3
131    );
132
133 ----------------------------------------------------------------
134 9: SELECTing a wild card
135
136 SELECT
137    "aou".*
138 FROM
139    actor.org_unit AS "aou";
140
141 ------------------------------------------------------------------
142 10: ORDER BY clause
143
144 SELECT
145    "aou".id AS "id",
146    "aou".parent_ou AS "parent",
147    "aou".shortname AS "short_name"
148 FROM
149    actor.org_unit AS "aou"
150 ORDER BY
151    "aou".parent_ou,
152    "aou".id;
153
154 --------------------------------------------------------------------
155 11: Looking up table name in IDL
156
157 SELECT
158    "aou".id AS "id"
159 FROM
160    actor.org_unit AS "aou";
161
162 --------------------------------------------------------------------
163 12: numeric bind variable (also works if default value is available)
164
165 SELECT
166    "aou".id,
167    "aou".name,
168    "aou".shortname,
169    "aou".opac_visible,
170    "aou".parent_ou
171 FROM
172    actor.org_unit AS "aou"
173 WHERE
174    "aou".id = :ou;
175
176 --------------------------------------------------------------------
177 13: string bind variable (using default value 'BRE' for :shortname)
178
179 SELECT
180    "aou".id,
181    "aou".name,
182    "aou".shortname,
183    "aou".opac_visible,
184    "aou".parent_ou
185 FROM
186    actor.org_unit AS "aou"
187 WHERE
188    "aou".shortname = 'BR3';
189
190 --------------------------------------------------------------------
191 14: IS NULL expression
192
193 SELECT
194    "aou".id
195 FROM
196    actor.org_unit AS "aou"
197 WHERE
198    "aou".parent_ou IS NULL;
199
200 --------------------------------------------------------------------
201 15: Series expression (chain of ORs)
202
203 SELECT
204    "aou".id AS "id"
205 FROM
206    actor.org_unit AS "aou"
207 WHERE
208    "aou".parent_ou IS NULL
209    OR "aou".email IS NULL
210    OR "aou".holds_address IS NULL;
211
212 --------------------------------------------------------------------
213 16: IN list
214
215 SELECT
216    "aou".id AS "id"
217 FROM
218    actor.org_unit AS "aou"
219 WHERE
220    "aou".parent_ou IN (1, 3, 6);
221
222 --------------------------------------------------------------------
223 17: Function call
224
225 SELECT
226    "aou".id AS "id",
227    "aou".name AS "name",
228    upper("aou".name) AS "name"
229 FROM
230    actor.org_unit AS "aou"
231 WHERE
232    "aou".parent_ou IN (1, 3, 6);
233
234 --------------------------------------------------------------------
235 18: Function call with subfield
236
237 SELECT
238    "aou".id AS "id",
239    "aou".name AS "id",
240    (actor.org_unit_ancestors("aou".id))."name" AS "root_name"
241 FROM
242    actor.org_unit AS "aou"
243 WHERE
244    "aou".id = 3;
245
246 --------------------------------------------------------------------
247 19: BETWEEN expression
248
249 SELECT
250    "aou".id AS "id"
251 FROM
252    actor.org_unit AS "aou"
253 WHERE
254    "aou".parent_ou BETWEEN 1 AND 4;
255
256 --------------------------------------------------------------------
257 20: EXTRACT and CURRENT_DATE functions
258
259 SELECT
260    "au".id AS "id",
261    EXTRACT(DOW FROM "au".create_date) AS "create_day",
262    CURRENT_DATE  AS "today"
263 FROM
264    actor.usr AS "au";
265
266 --------------------------------------------------------------------
267 21: GROUP BY, with aggregate function last
268
269 SELECT
270    "aou".parent_ou AS "parent",
271    COUNT(1) AS "how_many"
272 FROM
273    actor.org_unit AS "aou"
274 GROUP BY 1;
275
276 --------------------------------------------------------------------
277 22: GROUP BY, with aggregate function first
278
279 SELECT
280    COUNT(1) AS "how_many",
281    "aou".parent_ou AS "parent"
282 FROM
283    actor.org_unit AS "aou"
284 GROUP BY 2;
285
286 --------------------------------------------------------------------
287 23: Function in the FROM clause
288
289 SELECT
290    *
291 FROM
292    upper('goober');
293
294 --------------------------------------------------------------------
295 24: CASE expression
296
297 SELECT
298    "aou".id AS "id",
299    CASE "aou".id
300       WHEN 1
301          THEN 'First'
302       WHEN 2
303          THEN 'Second'
304       ELSE
305          'Other'
306    END AS "Branch sequence"
307 FROM
308    actor.org_unit AS "aou";
309
310 --------------------------------------------------------------------
311 25: CAST expression
312
313 SELECT
314    CAST ("aou".id AS TEXT) AS "cast_text"
315 FROM
316    actor.org_unit AS "aou";