[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2575
  • Last Modified:

crosstab with oracle

How can i get a crosstab with oracle sql ?
0
pifede
Asked:
pifede
1 Solution
 
scjonsonCommented:
In order to get a crosstab in SQL you need to use the Decode statement.  The values accross the top need to be known.  The values down the left will be the column which is grouped by.

If an individual table has a column for age and a column for gender here's what you'd do:

SELECT Age,
              sum(decode(v_gender,'F',1,0)) "Female",
              sum(decode(v_gender,'M','1',0)) "Male"
FROM individual
group by age

The results would look like:
      Age    Female      Male
     ---------    ---------     ---------
       17         1              2
       18         0              4
       19         3            11
       20        11           19
       21        14           21
       22        12             9
       23         5            20
    etc.

To make a dynamic crosstab report dynamic SQL would have to be used.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now