Solved

Combining the output of 2 oracle queries

Posted on 2016-09-26
2
65 Views
Last Modified: 2016-09-26
Is there a way I can output the results of both queries stacked on top of one another?
The desired results would look the same as running the queries seperately except with the results of both queries given on top of eachover by running "one" query instead of "two"

The results would look something like
School Name, EGRADE1, EGRADE2, EGRADE3, Total Student Enrollment
SCHOOL1, 20, 21, 22, 63
SCHOOL2, 30, 31, 32, 93
SCHOOL3, 40, 41, 42, 123
 , 90, 93, 96, 279

School Name, SGRADE1, SGRADE2, SGRADE3, Total SPED Enrollment
SCHOOL1, 5, 6, 7, 18
SCHOOL2, 4, 5, 6, 15
SCHOOL3, 3, 4, 5, 12
 , 12, 15, 18, 45

Open in new window


Query1
select SCHOOLID as "School Name", 
COUNT(CASE WHEN grade_level = '1' THEN 1 end) "EGRADE1",
COUNT(CASE WHEN grade_level = '2' THEN 1 end) "EGRADE2",
COUNT(CASE WHEN grade_level = '3' THEN 1 end) "EGRADE2",
COUNT(*) "Total Student Enrollment",
from students
where enroll_status=0 AND SCHOOLID IN (SCHOOL1, SCHOOL2, SCHOOL3, SCHOOL4)
group by Rollup(SCHOOLID)
order by SCHOOLID

Open in new window

Query2
select SCHOOLID as "School Name", 
COUNT(CASE WHEN grade_level = '1' AND S_CT_STU_LANGUAGE_X.ELLINDICATOR = 'Y' THEN 1 end) "SGRADE1",
COUNT(CASE WHEN grade_level = '2' AND S_CT_STU_LANGUAGE_X.ELLINDICATOR = 'Y' THEN 1 end) "SGRADE2",
COUNT(CASE WHEN grade_level = '3' AND S_CT_STU_LANGUAGE_X.ELLINDICATOR = 'Y' THEN 1 end) "SGRADE3",
COUNT(CASE WHEN S_CT_STU_LANGUAGE_X.ELLINDICATOR = 'Y' THEN 1 end) "Total SPED Enrollment"
from students
LEFT JOIN PS.S_CT_STU_LANGUAGE_X S_CT_STU_LANGUAGE_X ON STUDENTS.DCID = S_CT_STU_LANGUAGE_X.STUDENTSDCID
where enroll_status=0 AND SCHOOLID IN (SCHOOL1, SCHOOL2, SCHOOL3, SCHOOL4)
group by Rollup(SCHOOLID)
order by SCHOOLID

Open in new window

0
Comment
Question by:Basssque
2 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 41816584
As long as the number of columns and data types between the queries are the same, just UNION or "UNION ALL" them

select ... --query1
union all
select ... --query2
0
 

Author Comment

by:Basssque
ID: 41816599
That works great thanks!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

777 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