Solved

using order by with union all - oracly query

Posted on 2016-10-06
2
24 Views
Last Modified: 2016-10-25
Using the query below, I'm trying to set the order of the grade_level column.  If I put order by grade_level in the query I want to set the order in, it says the query isn't ended properly.  If I put it at the very bottom, is says the field is invalid.  I'm stuck on this one, any help is appreciated.  thanks!


UNION ALL
SELECT 'Totals (schools above only)' as School,null,null,null,null,null,null,null,null,null,null,null,null,null,null FROM dual
UNION ALL
SELECT 
null,grade_level,null,null,null,null,null,null,null,null,
COUNT(CASE WHEN S_CT_STU_DEMOGRAPHICS_X.GIFTEDTALENTED = '09' THEN 1 end) ALP,
COUNT(CASE WHEN S_CT_STU_SPED_X.SPECIALEDUCATION = 'Y' THEN 1 end) SPED,
COUNT(CASE WHEN ETHNICITY <> '04' THEN 1 end) Minority,
COUNT(CASE WHEN S_CT_STU_LANGUAGE_X.ELLINDICATOR = 'Y' THEN 1 end) ESOL,
COUNT(student_number) cnt
FROM students
LEFT JOIN PS.S_CT_STU_SPED_X S_CT_STU_SPED_X ON STUDENTS.DCID = S_CT_STU_SPED_X.STUDENTSDCID
LEFT JOIN PS.S_CT_STU_LANGUAGE_X S_CT_STU_LANGUAGE_X ON STUDENTS.DCID = S_CT_STU_LANGUAGE_X.STUDENTSDCID
LEFT JOIN PS.S_CT_STU_DEMOGRAPHICS_X S_CT_STU_DEMOGRAPHICS_X ON STUDENTS.DCID = S_CT_STU_DEMOGRAPHICS_X.STUDENTSDCID
WHERE enroll_status = 0 AND schoolid IN (3, 4, 8, 9, 10, 13, 14, 15, 22) AND lower(last_name) != 'doe'
GROUP BY grade_level
UNION ALL
SELECT 
'totals' as totals,null,null,null,null,null,null,null,null,null,
COUNT(CASE WHEN S_CT_STU_DEMOGRAPHICS_X.GIFTEDTALENTED = '09' THEN 1 end) ALP,
COUNT(CASE WHEN S_CT_STU_SPED_X.SPECIALEDUCATION = 'Y' THEN 1 end) SPED,
COUNT(CASE WHEN ETHNICITY <> '04' THEN 1 end) Minority,
COUNT(CASE WHEN S_CT_STU_LANGUAGE_X.ELLINDICATOR = 'Y' THEN 1 end) ESOL,
COUNT(student_number) cnt
FROM students
LEFT JOIN PS.S_CT_STU_SPED_X S_CT_STU_SPED_X ON STUDENTS.DCID = S_CT_STU_SPED_X.STUDENTSDCID
LEFT JOIN PS.S_CT_STU_LANGUAGE_X S_CT_STU_LANGUAGE_X ON STUDENTS.DCID = S_CT_STU_LANGUAGE_X.STUDENTSDCID
LEFT JOIN PS.S_CT_STU_DEMOGRAPHICS_X S_CT_STU_DEMOGRAPHICS_X ON STUDENTS.DCID = S_CT_STU_DEMOGRAPHICS_X.STUDENTSDCID
WHERE enroll_status = 0 AND schoolid IN (3, 4, 8, 9, 10, 13, 14, 15, 22) AND lower(last_name) != 'doe'
UNION ALL
SELECT 
'total percentages' as percentage,null,null,null,null,null,null,null,null,null,
ROUND(COUNT(CASE WHEN S_CT_STU_DEMOGRAPHICS_X.GIFTEDTALENTED = '09' THEN 1 end)/ count(*) * 100) ALP,
ROUND(COUNT(CASE WHEN S_CT_STU_SPED_X.SPECIALEDUCATION = 'Y' THEN 1 end)/ count(*) * 100) SPED,
ROUND(COUNT(CASE WHEN ETHNICITY <> '04' THEN 1 end)/ count(*) * 100) Minority,
ROUND(COUNT(CASE WHEN S_CT_STU_LANGUAGE_X.ELLINDICATOR = 'Y' THEN 1 end)/ count(*) * 100) ESOL,
null
FROM students
LEFT JOIN PS.S_CT_STU_SPED_X S_CT_STU_SPED_X ON STUDENTS.DCID = S_CT_STU_SPED_X.STUDENTSDCID
LEFT JOIN PS.S_CT_STU_LANGUAGE_X S_CT_STU_LANGUAGE_X ON STUDENTS.DCID = S_CT_STU_LANGUAGE_X.STUDENTSDCID
LEFT JOIN PS.S_CT_STU_DEMOGRAPHICS_X S_CT_STU_DEMOGRAPHICS_X ON STUDENTS.DCID = S_CT_STU_DEMOGRAPHICS_X.STUDENTSDCID
WHERE enroll_status = 0 AND schoolid IN (3, 4, 8, 9, 10, 13, 14, 15, 22) AND lower(last_name) != 'doe'

Open in new window

0
Comment
Question by:Basssque
2 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points (awarded by participants)
ID: 41832266
You can only have one order by on a unioned query.

Try simplified tests to see this.

This will reproduce the same error you described:
select 'hello' col from dual order by 1
union all
select 'world' from dual
/

Open in new window


Think about it this way:  The union says to treat all select statements as one query.  The order by is for the entire result set.

Add only one order by ad the end of all the unions:
select 'hello' col from dual
union all
select 'world' from dual
order by 1
/

Open in new window


That orders the results of both queries not just the second one.
2

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now