Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using field values to generate columns - oracle query

Posted on 2016-10-04
6
Medium Priority
?
99 Views
Last Modified: 2016-10-06
Using the following query, how could I take any field values from the homeroom column and use those values to create new columns based on the number of results (see examples below)?  I've used 3 homerooms in my example but in some cases there could be up to 10 per grade. Thanks!!

SELECT
grade_level as "Grade Level",
home_room,
COUNT(*) "Total Enrolled"
from students
where 
enroll_status=0 
AND schoolid IN (3)
group by home_room, grade_level

Open in new window


current results
Grade Level, HOME_ROOM, Total Enrolled
3, homeroom1, 17
3, homeroom2, 14
4, homeroom3, 15
5, homeroom4, 16
5, homeroom5, 18
5, homeroom6, 19

Open in new window


desired results
Grade Level, 1st Result, 2nd Result, 3rd Result, Total Enrolled
3, 17, 14, , 31
4, 15, , , 15
5, 16, 18, 19, 53

Open in new window

0
Comment
Question by:Basssque
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 41828711
  SELECT grade_level,
         MAX(CASE WHEN rn = 1 THEN cnt END) first_result,
         MAX(CASE WHEN rn = 2 THEN cnt END) second_result,
         MAX(CASE WHEN rn = 3 THEN cnt END) third_result,
         SUM(cnt)                         total_enrolled
    FROM (SELECT q.*, ROW_NUMBER() OVER(PARTITION BY grade_level ORDER BY home_room) rn
            FROM (  SELECT grade_level, home_room, COUNT(*) cnt
                      FROM students
                     WHERE enroll_status = 0 AND schoolid IN (3)
                  GROUP BY home_room, grade_level) q)
GROUP BY grade_level
ORDER BY grade_level;

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41828736
... and should you wonder: There is no way to have a dynamic number of columns. You always need to code for the max number of columns, even if you use the Pivot syntax not shown yet.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41828739
a better version of what I posted above,
the first uses your original query this skips the extra layer of aggregation.

  SELECT grade_level,
         NULLIF(COUNT(CASE WHEN rn = 1 THEN 1 END), 0) first_result,
         NULLIF(COUNT(CASE WHEN rn = 2 THEN 1 END), 0) second_result,
         NULLIF(COUNT(CASE WHEN rn = 3 THEN 1 END), 0) third_result,
         COUNT(*)                                    total_enrolled
    FROM (SELECT s.*, DENSE_RANK() OVER(PARTITION BY grade_level ORDER BY home_room) rn
            FROM students s)
GROUP BY grade_level
ORDER BY grade_level;

Open in new window


and qlemo is correct, there isn't a way to create a dynamic list of columns using only sql
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 

Author Comment

by:Basssque
ID: 41830551
sdstuber,

The first query worked
The second query didn't return any results except in the total column.
Is there a way to prevent columns from being rendered if there are no results in a column?  Thanks!!
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 41830716
>>> Is there a way to prevent columns from being rendered

No, that's another form of creating a dynamic result set which is not possible within SQL.
The reason is - a SQL statement must know the columns it is going to return when it is parsed (i.e. before it executes.)  So, trying to base the columns on the results of the query will never work using SQL alone.

The only way to do what you want is to use a reporting tool that can evaluate the result set and only display columns as needed,  or use pl/sql or some other language to pre-evaluate the results (generally by executing some variation of the query first) then constructing a specific sql query to return exactly what you want.  Again, this is not really a sql solution then.


>>> The second query didn't return any results except in the total column.

using what sample data?
It tested both queries using your sample data and they both worked.
0
 

Author Closing Comment

by:Basssque
ID: 41831971
Thank you again for your time!
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows how to recover a database from a user managed backup
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question