Home Explore Blog CI



postgresql

2nd chunk of `doc/src/sgml/tablefunc.sgml`
e2a1212ef703dbd471df63d210b4386c107e6854c0d349f30000000100000fa3
 role="func_table_entry"><para role="func_signature">
        <function>crosstab</function> ( <parameter>source_sql</parameter> <type>text</type>, <parameter>category_sql</parameter> <type>text</type> )
        <returnvalue>setof record</returnvalue>
       </para>
       <para>
        Produces a <quote>pivot table</quote>
        with the value columns specified by a second query.
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <function>crosstab</function> ( <parameter>sql</parameter> <type>text</type>, <parameter>N</parameter> <type>integer</type> )
        <returnvalue>setof record</returnvalue>
       </para>
       <para>
        Obsolete version of <function>crosstab(text)</function>.
        The parameter <parameter>N</parameter> is now ignored, since the
        number of value columns is always determined by the calling query.
       </para></entry>
      </row>

      <row>
       <entry role="func_table_entry"><para role="func_signature">
        <indexterm><primary>connectby</primary></indexterm>
        <function>connectby</function> ( <parameter>relname</parameter> <type>text</type>, <parameter>keyid_fld</parameter> <type>text</type>, <parameter>parent_keyid_fld</parameter> <type>text</type>
        <optional>, <parameter>orderby_fld</parameter> <type>text</type> </optional>, <parameter>start_with</parameter> <type>text</type>, <parameter>max_depth</parameter> <type>integer</type>
        <optional>, <parameter>branch_delim</parameter> <type>text</type> </optional> )
        <returnvalue>setof record</returnvalue>
       </para>
       <para>
        Produces a representation of a hierarchical tree structure.
        </para></entry>
      </row>
     </tbody>
    </tgroup>
  </table>

  <sect3 id="tablefunc-functions-normal-rand">
   <title><function>normal_rand</function></title>

   <indexterm>
    <primary>normal_rand</primary>
   </indexterm>

<synopsis>
normal_rand(int numvals, float8 mean, float8 stddev) returns setof float8
</synopsis>

    <para>
     <function>normal_rand</function> produces a set of normally distributed random
     values (Gaussian distribution).
    </para>

    <para>
     <parameter>numvals</parameter> is the number of values to be returned
     from the function. <parameter>mean</parameter> is the mean of the normal
     distribution of values and <parameter>stddev</parameter> is the standard
     deviation of the normal distribution of values.
    </para>

    <para>
     For example, this call requests 1000 values with a mean of 5 and a
     standard deviation of 3:
    </para>

<screen>
test=# SELECT * FROM normal_rand(1000, 5, 3);
     normal_rand
----------------------
     1.56556322244898
     9.10040991424657
     5.36957140345079
   -0.369151492880995
    0.283600703686639
       .
       .
       .
     4.82992125404908
     9.71308014517282
     2.49639286969028
(1000 rows)
</screen>
  </sect3>

  <sect3 id="tablefunc-functions-crosstab-text">
   <title><function>crosstab(text)</function></title>

   <indexterm>
    <primary>crosstab</primary>
   </indexterm>

<synopsis>
crosstab(text sql)
crosstab(text sql, int N)
</synopsis>

   <para>
    The <function>crosstab</function> function is used to produce <quote>pivot</quote>
    displays, wherein data is listed across the page rather than down.
    For example, we might have data like
<programlisting>
row1    val11
row1    val12
row1    val13
...
row2    val21
row2    val22
row2    val23
...
</programlisting>
    which we wish to display like
<programlisting>
row1    val11   val12   val13   ...
row2    val21   val22   val23   ...
...
</programlisting>
    The <function>crosstab</function> function takes a text parameter that is an SQL
    query producing raw data formatted in the first way, and produces a table
    formatted in the second way.
   </para>

   <para>
    The <parameter>sql</parameter> parameter is an SQL statement that produces
    the

Title: tablefunc Module Functions: Crosstab and Connectby
Summary
This section describes several functions within the tablefunc module, including variations of the crosstab function for creating pivot tables and the connectby function for representing hierarchical tree structures. It also provides details on normal_rand, which generates a set of normally distributed random values. Specific examples and explanations of the crosstab function and its parameters are provided, showing how to transform raw data into a pivot table format.