Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The functionality you're after is called "pivot"--it's converting row data into columnar data. The opposite is called "unpivot". There is PIVOT/UNPIVOT syntax, but it isn't available until Oracle 11g.</p> <p>On Oracle 9i+, <a href="http://techonthenet.com/oracle/functions/case.php" rel="nofollow">CASE statements</a> are used while prior to that you need to use <a href="http://techonthenet.com/oracle/functions/decode.php" rel="nofollow">DECODE</a> to construct the logic so the values come out in columns. Here's an example for addressing if the limit to LEVEL is five on Oracle 9i+:</p> <pre><code> SELECT MAX(CASE WHEN LEVEL = 1 THEN LEVEL END) AS one, MAX(CASE WHEN LEVEL = 2 THEN LEVEL END) AS two, MAX(CASE WHEN LEVEL = 3 THEN LEVEL END) AS three, MAX(CASE WHEN LEVEL = 4 THEN LEVEL END) AS four, MAX(CASE WHEN LEVEL = 5 THEN LEVEL END) AS five FROM DUAL CONNECT BY LEVEL &lt;= 5 </code></pre> <p>The aggregate function MAX is necessary to "flatten" the resultset into a single row/record.</p> <p>If you got this far, you're thinking "but I don't want to have to specify for every column--I want it to be dynamic based on <em>i</em>...". There's two issues:</p> <ol> <li>There is a limit to the number of columns - see <a href="http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96525/e1500.htm#1001271" rel="nofollow">ORA-01792 maximum number of columns in a table or view is 1000</a>. That goes for 8i &amp; 9i, anyways. And frankly, needing to generate that many columns raises serious questions about what you're trying to do with the resultset, if there might be a better approach...</li> <li><a href="http://download.oracle.com/docs/cd/B10500_01/appdev.920/a96590/adg09dyn.htm" rel="nofollow">Dynamic requirements means using dynamic SQL</a></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload