title: PostgreSQL crosstab function
body: <p>I have some problem with crosstab function.</p>
  
  <p>My table "t" is </p>
  
  <pre><code>date;name;hour;cause;c_p
  "2013-06-12";167;14;0;2
  "2013-06-12";167;16;0;3
  "2013-06-12";167;16;0;4
  "2013-06-12";167;19;1;1
  "2013-06-12";167;19;0;4
  </code></pre>
  
  <p>I'd have this "pivot table" t_pivot</p>
  
  <pre><code>day;name;hour;cause_0;cause_1
  "2013-06-12";167;14;2;0 -----sum(c_p)
  "2013-06-12";167;16;7;0
  "2013-06-12";167;19;4;1
  </code></pre>
  
  <p>Sql Code is</p>
  
  <pre><code>SELECT  * from crosstab (
      'SELECT  day,name,hour,cause, SUM(c_p) AS c_p
          FROM t
          GROUP BY 1,2,3,4
          ORDER BY 3 ',
  
       'SELECT DISTINCT cause 
           FROM i
           ORDER BY 1')
  
  AS t_pivot (day date, name integer,hour integer, cause_0 integer,cause_1 integer)
  </code></pre>
  
  <p>Query result is one row table depending by the "ORDER BY"</p>
  
  <pre><code>ORDER BY 3
  "2013-06-12";167;14;4;1
  
  ORDER BY 1, ORDER BY 2
  "2013-06-12";167;14;7;1
  </code></pre>
  
  <p>Where is the error?
  Thanks
  f.</p>