~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to contrib/tablefunc/README.tablefunc

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * tablefunc
 
3
 *
 
4
 * Sample to demonstrate C functions which return setof scalar
 
5
 * and setof composite.
 
6
 * Joe Conway <mail@joeconway.com>
 
7
 * And contributors:
 
8
 * Nabil Sayegh <postgresql@e-trolley.de>
 
9
 *
 
10
 * Copyright (c) 2002-2005, PostgreSQL Global Development Group
 
11
 *
 
12
 * Permission to use, copy, modify, and distribute this software and its
 
13
 * documentation for any purpose, without fee, and without a written agreement
 
14
 * is hereby granted, provided that the above copyright notice and this
 
15
 * paragraph and the following two paragraphs appear in all copies.
 
16
 * 
 
17
 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
 
18
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
 
19
 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
 
20
 * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
 
21
 * POSSIBILITY OF SUCH DAMAGE.
 
22
 * 
 
23
 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
 
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 
25
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 
26
 * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
 
27
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 
28
 *
 
29
 */
 
30
Version 0.1 (20 July, 2002):
 
31
  First release
 
32
 
 
33
Release Notes:
 
34
 
 
35
  Version 0.1
 
36
    - initial release    
 
37
 
 
38
Installation:
 
39
  Place these files in a directory called 'tablefunc' under 'contrib' in the
 
40
  PostgreSQL source tree. Then run:
 
41
 
 
42
    make
 
43
    make install
 
44
 
 
45
  You can use tablefunc.sql to create the functions in your database of choice, e.g.
 
46
 
 
47
    psql -U postgres template1 < tablefunc.sql
 
48
 
 
49
  installs following functions into database template1:
 
50
 
 
51
    normal_rand(int numvals, float8 mean, float8 stddev)
 
52
      - returns a set of normally distributed float8 values
 
53
 
 
54
    crosstabN(text sql)
 
55
      - returns a set of row_name plus N category value columns
 
56
      - crosstab2(), crosstab3(), and crosstab4() are defined for you,
 
57
        but you can create additional crosstab functions per the instructions
 
58
        in the documentation below.
 
59
 
 
60
    crosstab(text sql, N int)
 
61
      - returns a set of row_name plus N category value columns
 
62
      - requires anonymous composite type syntax in the FROM clause. See
 
63
        the instructions in the documentation below.
 
64
 
 
65
    connectby(text relname, text keyid_fld, text parent_keyid_fld
 
66
                [, text orderby_fld], text start_with, int max_depth
 
67
                                                                [, text branch_delim])
 
68
      - returns keyid, parent_keyid, level, and an optional branch string
 
69
        and an optional serial column for ordering siblings
 
70
      - requires anonymous composite type syntax in the FROM clause. See
 
71
        the instructions in the documentation below.
 
72
 
 
73
Documentation
 
74
==================================================================
 
75
Name
 
76
 
 
77
normal_rand(int, float8, float8) - returns a set of normally
 
78
       distributed float8 values
 
79
 
 
80
Synopsis
 
81
 
 
82
normal_rand(int numvals, float8 mean, float8 stddev)
 
83
 
 
84
Inputs
 
85
 
 
86
  numvals
 
87
    the number of random values to be returned from the function
 
88
 
 
89
  mean
 
90
    the mean of the normal distribution of values
 
91
 
 
92
  stddev
 
93
    the standard deviation of the normal distribution of values
 
94
 
 
95
Outputs
 
96
 
 
97
  Returns setof float8, where the returned set of random values are normally
 
98
    distributed (Gaussian distribution)
 
99
 
 
100
Example usage
 
101
 
 
102
  test=# SELECT * FROM
 
103
  test=# normal_rand(1000, 5, 3);
 
104
     normal_rand
 
105
----------------------
 
106
     1.56556322244898
 
107
     9.10040991424657
 
108
     5.36957140345079
 
109
   -0.369151492880995
 
110
    0.283600703686639
 
111
       .
 
112
       .
 
113
       .
 
114
     4.82992125404908
 
115
     9.71308014517282
 
116
     2.49639286969028
 
117
(1000 rows)
 
118
 
 
119
  Returns 1000 values with a mean of 5 and a standard deviation of 3.
 
120
 
 
121
==================================================================
 
122
Name
 
123
 
 
124
crosstabN(text) - returns a set of row_name plus N category value columns
 
125
 
 
126
Synopsis
 
127
 
 
128
crosstabN(text sql)
 
129
 
 
130
Inputs
 
131
 
 
132
  sql
 
133
 
 
134
    A SQL statement which produces the source set of data. The SQL statement
 
135
    must return one row_name column, one category column, and one value
 
136
    column.
 
137
 
 
138
    e.g. provided sql must produce a set something like:
 
139
 
 
140
             row_name    cat    value
 
141
            ----------+-------+-------
 
142
              row1      cat1    val1
 
143
              row1      cat2    val2
 
144
              row1      cat3    val3
 
145
              row1      cat4    val4
 
146
              row2      cat1    val5
 
147
              row2      cat2    val6
 
148
              row2      cat3    val7
 
149
              row2      cat4    val8
 
150
 
 
151
Outputs
 
152
 
 
153
  Returns setof tablefunc_crosstab_N, which is defined by:
 
154
 
 
155
    CREATE VIEW tablefunc_crosstab_N AS
 
156
      SELECT
 
157
        ''::TEXT AS row_name,
 
158
        ''::TEXT AS category_1,
 
159
        ''::TEXT AS category_2,
 
160
            .
 
161
            .
 
162
            .
 
163
        ''::TEXT AS category_N;
 
164
 
 
165
     for the default installed functions, where N is 2, 3, or 4.
 
166
 
 
167
     e.g. the provided crosstab2 function produces a set something like:
 
168
                      <== values  columns ==>
 
169
           row_name   category_1   category_2
 
170
           ---------+------------+------------
 
171
             row1        val1         val2
 
172
             row2        val5         val6
 
173
 
 
174
Notes
 
175
 
 
176
  1. The sql result must be ordered by 1,2.
 
177
 
 
178
  2. The number of values columns depends on the tuple description
 
179
     of the function's declared return type.
 
180
 
 
181
  3. Missing values (i.e. not enough adjacent rows of same row_name to
 
182
     fill the number of result values columns) are filled in with nulls.
 
183
 
 
184
  4. Extra values (i.e. too many adjacent rows of same row_name to fill
 
185
     the number of result values columns) are skipped.
 
186
 
 
187
  5. Rows with all nulls in the values columns are skipped.
 
188
 
 
189
  6. The installed defaults are for illustration purposes. You
 
190
     can create your own return types and functions based on the
 
191
     crosstab() function of the installed library.
 
192
 
 
193
     The return type must have a first column that matches the data
 
194
     type of the sql set used as its source. The subsequent category
 
195
     columns must have the same data type as the value column of the
 
196
     sql result set.
 
197
 
 
198
     Create a VIEW to define your return type, similar to the VIEWS
 
199
     in the provided installation script. Then define a unique function
 
200
     name accepting one text parameter and returning setof your_view_name.
 
201
     For example, if your source data produces row_names that are TEXT,
 
202
     and values that are FLOAT8, and you want 5 category columns:
 
203
 
 
204
      CREATE VIEW my_crosstab_float8_5_cols AS
 
205
        SELECT
 
206
          ''::TEXT AS row_name,
 
207
          0::FLOAT8 AS category_1,
 
208
          0::FLOAT8 AS category_2,
 
209
          0::FLOAT8 AS category_3,
 
210
          0::FLOAT8 AS category_4,
 
211
          0::FLOAT8 AS category_5;
 
212
 
 
213
      CREATE OR REPLACE FUNCTION crosstab_float8_5_cols(text)
 
214
        RETURNS setof my_crosstab_float8_5_cols
 
215
        AS '$libdir/tablefunc','crosstab' LANGUAGE 'c' STABLE STRICT;
 
216
 
 
217
Example usage
 
218
 
 
219
create table ct(id serial, rowclass text, rowid text, attribute text, value text);
 
220
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att1','val1');
 
221
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att2','val2');
 
222
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att3','val3');
 
223
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att4','val4');
 
224
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att1','val5');
 
225
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att2','val6');
 
226
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att3','val7');
 
227
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att4','val8');
 
228
 
 
229
select * from crosstab3(
 
230
  'select rowid, attribute, value
 
231
   from ct
 
232
   where rowclass = ''group1''
 
233
   and (attribute = ''att2'' or attribute = ''att3'') order by 1,2;');
 
234
 
 
235
 row_name | category_1 | category_2 | category_3
 
236
----------+------------+------------+------------
 
237
 test1    | val2       | val3       |
 
238
 test2    | val6       | val7       |
 
239
(2 rows)
 
240
 
 
241
==================================================================
 
242
Name
 
243
 
 
244
crosstab(text, int) - returns a set of row_name
 
245
                      plus N category value columns
 
246
 
 
247
Synopsis
 
248
 
 
249
crosstab(text sql, int N)
 
250
 
 
251
Inputs
 
252
 
 
253
  sql
 
254
 
 
255
    A SQL statement which produces the source set of data. The SQL statement
 
256
    must return one row_name column, one category column, and one value
 
257
    column.
 
258
 
 
259
    e.g. provided sql must produce a set something like:
 
260
 
 
261
             row_name    cat    value
 
262
            ----------+-------+-------
 
263
              row1      cat1    val1
 
264
              row1      cat2    val2
 
265
              row1      cat3    val3
 
266
              row1      cat4    val4
 
267
              row2      cat1    val5
 
268
              row2      cat2    val6
 
269
              row2      cat3    val7
 
270
              row2      cat4    val8
 
271
 
 
272
  N
 
273
 
 
274
    number of category value columns
 
275
 
 
276
Outputs
 
277
 
 
278
  Returns setof record, which must defined with a column definition
 
279
  in the FROM clause of the SELECT statement, e.g.:
 
280
 
 
281
    SELECT *
 
282
    FROM crosstab(sql, 2) AS ct(row_name text, category_1 text, category_2 text);
 
283
 
 
284
    the example crosstab function produces a set something like:
 
285
                      <== values  columns ==>
 
286
           row_name   category_1   category_2
 
287
           ---------+------------+------------
 
288
             row1        val1         val2
 
289
             row2        val5         val6
 
290
 
 
291
Notes
 
292
 
 
293
  1. The sql result must be ordered by 1,2.
 
294
 
 
295
  2. The number of values columns is determined at run-time. The 
 
296
     column definition provided in the FROM clause must provide for
 
297
     N + 1 columns of the proper data types.
 
298
 
 
299
  3. Missing values (i.e. not enough adjacent rows of same row_name to
 
300
     fill the number of result values columns) are filled in with nulls.
 
301
 
 
302
  4. Extra values (i.e. too many adjacent rows of same row_name to fill
 
303
     the number of result values columns) are skipped.
 
304
 
 
305
  5. Rows with all nulls in the values columns are skipped.
 
306
 
 
307
 
 
308
Example usage
 
309
 
 
310
create table ct(id serial, rowclass text, rowid text, attribute text, value text);
 
311
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att1','val1');
 
312
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att2','val2');
 
313
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att3','val3');
 
314
insert into ct(rowclass, rowid, attribute, value) values('group1','test1','att4','val4');
 
315
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att1','val5');
 
316
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att2','val6');
 
317
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att3','val7');
 
318
insert into ct(rowclass, rowid, attribute, value) values('group1','test2','att4','val8');
 
319
 
 
320
SELECT *
 
321
FROM crosstab(
 
322
  'select rowid, attribute, value
 
323
   from ct
 
324
   where rowclass = ''group1''
 
325
   and (attribute = ''att2'' or attribute = ''att3'') order by 1,2;', 3)
 
326
AS ct(row_name text, category_1 text, category_2 text, category_3 text);
 
327
 
 
328
 row_name | category_1 | category_2 | category_3
 
329
----------+------------+------------+------------
 
330
 test1    | val2       | val3       |
 
331
 test2    | val6       | val7       |
 
332
(2 rows)
 
333
 
 
334
==================================================================
 
335
Name
 
336
 
 
337
crosstab(text, text) - returns a set of row_name, extra, and
 
338
                      category value columns
 
339
 
 
340
Synopsis
 
341
 
 
342
crosstab(text source_sql, text category_sql)
 
343
 
 
344
Inputs
 
345
 
 
346
  source_sql
 
347
 
 
348
    A SQL statement which produces the source set of data. The SQL statement
 
349
    must return one row_name column, one category column, and one value
 
350
    column. It may also have one or more "extra" columns.
 
351
 
 
352
    The row_name column must be first. The category and value columns
 
353
    must be the last two columns, in that order. "extra" columns must be
 
354
    columns 2 through (N - 2), where N is the total number of columns.
 
355
 
 
356
    The "extra" columns are assumed to be the same for all rows with the
 
357
    same row_name. The values returned are copied from the first row
 
358
    with a given row_name and subsequent values of these columns are ignored
 
359
    until row_name changes.
 
360
 
 
361
    e.g. source_sql must produce a set something like:
 
362
         SELECT row_name, extra_col, cat, value FROM foo;
 
363
 
 
364
             row_name    extra_col   cat    value
 
365
            ----------+------------+-----+---------
 
366
              row1         extra1    cat1    val1
 
367
              row1         extra1    cat2    val2
 
368
              row1         extra1    cat4    val4
 
369
              row2         extra2    cat1    val5
 
370
              row2         extra2    cat2    val6
 
371
              row2         extra2    cat3    val7
 
372
              row2         extra2    cat4    val8
 
373
 
 
374
  category_sql
 
375
 
 
376
    A SQL statement which produces the distinct set of categories. The SQL
 
377
    statement must return one category column only. category_sql must produce
 
378
    at least one result row or an error will be generated. category_sql
 
379
    must not produce duplicate categories or an error will be generated.
 
380
 
 
381
    e.g. SELECT DISTINCT cat FROM foo;
 
382
 
 
383
              cat
 
384
            -------
 
385
              cat1
 
386
              cat2
 
387
              cat3
 
388
              cat4
 
389
 
 
390
Outputs
 
391
 
 
392
  Returns setof record, which must be defined with a column definition
 
393
  in the FROM clause of the SELECT statement, e.g.:
 
394
 
 
395
    SELECT * FROM crosstab(source_sql, cat_sql)
 
396
    AS ct(row_name text, extra text, cat1 text, cat2 text, cat3 text, cat4 text);
 
397
 
 
398
    the example crosstab function produces a set something like:
 
399
                      <== values  columns ==>
 
400
           row_name   extra   cat1   cat2   cat3   cat4
 
401
           ---------+-------+------+------+------+------
 
402
             row1     extra1  val1   val2          val4
 
403
             row2     extra2  val5   val6   val7   val8
 
404
 
 
405
Notes
 
406
 
 
407
  1. source_sql must be ordered by row_name (column 1).
 
408
 
 
409
  2. The number of values columns is determined at run-time. The 
 
410
     column definition provided in the FROM clause must provide for
 
411
     the correct number of columns of the proper data types.
 
412
 
 
413
  3. Missing values (i.e. not enough adjacent rows of same row_name to
 
414
     fill the number of result values columns) are filled in with nulls.
 
415
 
 
416
  4. Extra values (i.e. source rows with category not found in category_sql
 
417
     result) are skipped.
 
418
 
 
419
  5. Rows with a null row_name column are skipped.
 
420
 
 
421
 
 
422
Example usage
 
423
 
 
424
create table cth(id serial, rowid text, rowdt timestamp, attribute text, val text);
 
425
insert into cth values(DEFAULT,'test1','01 March 2003','temperature','42');
 
426
insert into cth values(DEFAULT,'test1','01 March 2003','test_result','PASS');
 
427
insert into cth values(DEFAULT,'test1','01 March 2003','volts','2.6987');
 
428
insert into cth values(DEFAULT,'test2','02 March 2003','temperature','53');
 
429
insert into cth values(DEFAULT,'test2','02 March 2003','test_result','FAIL');
 
430
insert into cth values(DEFAULT,'test2','02 March 2003','test_startdate','01 March 2003');
 
431
insert into cth values(DEFAULT,'test2','02 March 2003','volts','3.1234');
 
432
 
 
433
SELECT * FROM crosstab
 
434
(
 
435
  'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
 
436
  'SELECT DISTINCT attribute FROM cth ORDER BY 1'
 
437
)
 
438
AS
 
439
(
 
440
       rowid text,
 
441
       rowdt timestamp,
 
442
       temperature int4,
 
443
       test_result text,
 
444
       test_startdate timestamp,
 
445
       volts float8
 
446
);
 
447
 rowid |          rowdt           | temperature | test_result |      test_startdate      | volts  
 
448
-------+--------------------------+-------------+-------------+--------------------------+--------
 
449
 test1 | Sat Mar 01 00:00:00 2003 |          42 | PASS        |                          | 2.6987
 
450
 test2 | Sun Mar 02 00:00:00 2003 |          53 | FAIL        | Sat Mar 01 00:00:00 2003 | 3.1234
 
451
(2 rows)
 
452
 
 
453
==================================================================
 
454
Name
 
455
 
 
456
connectby(text, text, text[, text], text, text, int[, text]) - returns a set
 
457
    representing a hierarchy (tree structure)
 
458
 
 
459
Synopsis
 
460
 
 
461
connectby(text relname, text keyid_fld, text parent_keyid_fld
 
462
            [, text orderby_fld], text start_with, int max_depth
 
463
                                                [, text branch_delim])
 
464
 
 
465
Inputs
 
466
 
 
467
  relname
 
468
 
 
469
    Name of the source relation
 
470
 
 
471
  keyid_fld
 
472
 
 
473
    Name of the key field
 
474
 
 
475
  parent_keyid_fld
 
476
 
 
477
    Name of the key_parent field
 
478
 
 
479
  orderby_fld
 
480
 
 
481
    If optional ordering of siblings is desired:
 
482
    Name of the field to order siblings
 
483
 
 
484
  start_with
 
485
 
 
486
    root value of the tree input as a text value regardless of keyid_fld type
 
487
 
 
488
  max_depth
 
489
 
 
490
    zero (0) for unlimited depth, otherwise restrict level to this depth
 
491
 
 
492
  branch_delim
 
493
 
 
494
    If optional branch value is desired, this string is used as the delimiter.
 
495
    When not provided, a default value of '~' is used for internal 
 
496
    recursion detection only, and no "branch" field is returned.
 
497
 
 
498
Outputs
 
499
 
 
500
  Returns setof record, which must defined with a column definition
 
501
  in the FROM clause of the SELECT statement, e.g.:
 
502
 
 
503
    SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~')
 
504
      AS t(keyid text, parent_keyid text, level int, branch text);
 
505
 
 
506
    - or -
 
507
 
 
508
    SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
 
509
      AS t(keyid text, parent_keyid text, level int);
 
510
                        
 
511
                - or -
 
512
 
 
513
    SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~')
 
514
      AS t(keyid text, parent_keyid text, level int, branch text, pos int);
 
515
 
 
516
                - or -
 
517
 
 
518
    SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0)
 
519
      AS t(keyid text, parent_keyid text, level int, pos int);
 
520
    
 
521
Notes
 
522
 
 
523
  1. keyid and parent_keyid must be the same data type
 
524
 
 
525
  2. The column definition *must* include a third column of type INT4 for
 
526
     the level value output
 
527
 
 
528
  3. If the branch field is not desired, omit both the branch_delim input
 
529
     parameter *and* the branch field in the query column definition. Note
 
530
     that when branch_delim is not provided, a default value of '~' is used
 
531
     for branch_delim for internal recursion detection, even though the branch
 
532
     field is not returned.
 
533
 
 
534
  4. If the branch field is desired, it must be the fourth column in the query
 
535
     column definition, and it must be type TEXT.
 
536
 
 
537
  5. The parameters representing table and field names must include double
 
538
     quotes if the names are mixed-case or contain special characters.
 
539
 
 
540
  6. If sorting of siblings is desired, the orderby_fld input parameter *and*
 
541
     a name for the resulting serial field (type INT32) in the query column
 
542
     definition must be given.
 
543
 
 
544
Example usage
 
545
 
 
546
CREATE TABLE connectby_tree(keyid text, parent_keyid text, pos int);
 
547
 
 
548
INSERT INTO connectby_tree VALUES('row1',NULL, 0);
 
549
INSERT INTO connectby_tree VALUES('row2','row1', 0);
 
550
INSERT INTO connectby_tree VALUES('row3','row1', 0);
 
551
INSERT INTO connectby_tree VALUES('row4','row2', 1);
 
552
INSERT INTO connectby_tree VALUES('row5','row2', 0);
 
553
INSERT INTO connectby_tree VALUES('row6','row4', 0);
 
554
INSERT INTO connectby_tree VALUES('row7','row3', 0);
 
555
INSERT INTO connectby_tree VALUES('row8','row6', 0);
 
556
INSERT INTO connectby_tree VALUES('row9','row5', 0);
 
557
 
 
558
-- with branch, without orderby_fld
 
559
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0, '~')
 
560
 AS t(keyid text, parent_keyid text, level int, branch text);
 
561
 keyid | parent_keyid | level |       branch
 
562
-------+--------------+-------+---------------------
 
563
 row2  |              |     0 | row2
 
564
 row4  | row2         |     1 | row2~row4
 
565
 row6  | row4         |     2 | row2~row4~row6
 
566
 row8  | row6         |     3 | row2~row4~row6~row8
 
567
 row5  | row2         |     1 | row2~row5
 
568
 row9  | row5         |     2 | row2~row5~row9
 
569
(6 rows)
 
570
 
 
571
-- without branch, without orderby_fld
 
572
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'row2', 0)
 
573
 AS t(keyid text, parent_keyid text, level int);
 
574
 keyid | parent_keyid | level
 
575
-------+--------------+-------
 
576
 row2  |              |     0
 
577
 row4  | row2         |     1
 
578
 row6  | row4         |     2
 
579
 row8  | row6         |     3
 
580
 row5  | row2         |     1
 
581
 row9  | row5         |     2
 
582
(6 rows)
 
583
 
 
584
-- with branch, with orderby_fld (notice that row5 comes before row4)
 
585
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~')
 
586
 AS t(keyid text, parent_keyid text, level int, branch text, pos int) ORDER BY t.pos;
 
587
 keyid | parent_keyid | level |       branch        | pos 
 
588
-------+--------------+-------+---------------------+-----
 
589
 row2  |              |     0 | row2                |   1
 
590
 row5  | row2         |     1 | row2~row5           |   2
 
591
 row9  | row5         |     2 | row2~row5~row9      |   3
 
592
 row4  | row2         |     1 | row2~row4           |   4
 
593
 row6  | row4         |     2 | row2~row4~row6      |   5
 
594
 row8  | row6         |     3 | row2~row4~row6~row8 |   6
 
595
(6 rows)
 
596
 
 
597
-- without branch, with orderby_fld (notice that row5 comes before row4)
 
598
SELECT * FROM connectby('connectby_tree', 'keyid', 'parent_keyid', 'pos', 'row2', 0)
 
599
 AS t(keyid text, parent_keyid text, level int, pos int) ORDER BY t.pos;
 
600
 keyid | parent_keyid | level | pos
 
601
-------+--------------+-------+-----
 
602
 row2  |              |     0 |   1
 
603
 row5  | row2         |     1 |   2
 
604
 row9  | row5         |     2 |   3
 
605
 row4  | row2         |     1 |   4
 
606
 row6  | row4         |     2 |   5
 
607
 row8  | row6         |     3 |   6
 
608
(6 rows)
 
609
 
 
610
==================================================================
 
611
-- Joe Conway
 
612