Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you know in advance all the values that you'll have for columns, then you can hardcode them in your query. Otherwise it gets a lot more complicated. Here it is in Oracle SQL:</p> <pre><code>with my_data as (select 'agra' city, 'atm' "type", 'X' "name" from dual union select 'agra' city, 'branch' "type", 'X1' "name" from dual union select 'delhi' city, 'atm' "type", 'X2' "name" from dual union select 'agra' city, 'atm' "type", 'X3' "name" from dual union select 'agra' city, 'atm' "type", 'X4' "name" from dual union select 'delhi' city, 'branch' "type", 'X5' "name" from dual union select 'chennai' city, 'branch' "type", 'X6' "name" from dual) select city, sum(decode("type", 'atm', 1, 0)) as ATM, sum(decode("type", 'branch', 1, 0)) as branch from my_data group by city; </code></pre> <p>If the number of columns is unknown, Oracle has a '<a href="http://www.oracle-developer.net/display.php?id=506" rel="nofollow">PIVOT XML</a>' statement that allows it to return all of the summarized data in one XML:</p> <pre><code>select * from my_data PIVOT XML (count("name") for "type" in (ANY)); </code></pre> <p>You can parse each column out yourself using the <a href="http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions052.htm" rel="nofollow">EXTRACTVALUE()</a> function, but this still implies that you know how many columns there are ahead of time. I believe that Oracle SQL doesn't have the ability to create a pivot table with a dynamic number of columns. It can be done in PL/SQL though, where you'd use a cursor to dynamically concatenate your SQL.</p>
    singulars
    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.
 

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