Help with subquery in SELECT clause?
-
I'm having difficulty with this query in Oracle SQL Developer. The way I have it makes sense in my mind, but my implementation must not be quite right. It's throwing a missing right parenthesis error, and when I remove the ORDER BY on line 9, it then throws the single-row subquery returns more than one row error. But I do need multiple rows to show up in that column. Can you please help me out? Query: List the location, number of sections taught in that location, and number of students enrolled in courses at that location. Sort by location. My implementation: SELECT Location, COUNT(s.Section_ID) AS Sections, **(SELECT COUNT(*) **FROM Enrollment E, Section s **WHERE e.Section_ID = s.Section_ID **AND Location IN ******(SELECT DISTINCT Location ******FROM Section) **GROUP BY Location **ORDER BY Location) AS Students FROM Enrollment E, Section s WHERE e.Section_ID = s.Section_ID GROUP BY Location ORDER BY Location Expected results: LOCATION / SECTIONS / STUDENTS H310 / 1 / 1 L206 / 1 / 8 L210 / 10 / 29 L211 / 3 / 10 ... Thanks for the help!
-
Answer:
This might be a bit clearer (assumes you have a lookup table of locations) SELECT l.Location, COALESCE(sCount, 0) AS Sections, COALESCE(eCount, 0) AS Students FROM Locations l LEFT OUTER JOIN (SELECT Location, COUNT(*) AS sCount FROM Section GROUP BY Location) s ON l.Location = s.Location LEFT OUTER JOIN (SELECT Location, COUNT(*) AS eCount FROM Enrollment GROUP BY Location) e ON l.Location = e.Location
JOEY!!!!! 【ツ】 at Yahoo! Answers Visit the source
Other answers
Well, the error is saying you are missing a closing parenthesis. If you make it more readable you can see what you are missing. Lastly, I don't believe you can have a nested query that returns a count and not use count in the outer where clause. Maybe something like this will work. SELECT Location, COUNT(s.Section_ID) AS Sections, FROM Enrollment E, Section s WHERE e.Section_ID = s.Section_ID AND e.Section_ID IN ( SELECT e.Section_ID FROM Enrollment E, Section s WHERE e.Section_ID = s.Section_ID AND Location IN ( SELECT DISTINCT Location FROM Section) GROUP BY Location ORDER BY Location ) AS Students ) GROUP BY Location ORDER BY Location
Related Q & A:
- How To Select Odd Numbers In Excel?Best solution by Yahoo! Answers
- How to select all articles and their similar articles from MySQL?Best solution by Stack Overflow
- How to write SQL statement with dynamic where clause?Best solution by Stack Overflow
- How to query with doctrine dql the where in clause?Best solution by Stack Overflow
- What is charterers contribution clause?Best solution by in.answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.