Solved

Need SQL help

Posted on 2011-02-23
6
224 Views
Last Modified: 2012-05-11
Have the following SQL queries that work fine

select COUNT(*) as AES_18
from students
where grade_level=4 AND Enroll_Status=0 AND SchoolID=5 AND home_room<>'SESO'
<td>~(AES_18)</td>

select COUNT(*) as AES_19
from Sections
where course_number=0400 AND termID=2000 AND SchoolID=5
<td>~(AES_19)</td>

I need to be able to use SQL to perform AES_18/AES_19
0
Comment
Question by:btcs1
[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
6 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34965275
SELECT
    (select COUNT(*) 
    from students
    where grade_level=4 AND Enroll_Status=0 AND SchoolID=5 AND home_room<>'SESO') / 
    (select COUNT(*) 
    from Sections
    where course_number=0400 AND termID=2000 AND SchoolID=5) AS Result

Open in new window

0
 
LVL 26

Expert Comment

by:tigin44
ID: 34965307

select
(
select COUNT(*) as AES_18
from students
where grade_level=4 AND Enroll_Status=0 AND SchoolID=5 AND home_room<>'SESO'
/

select COUNT(*) as AES_19
from Sections
where course_number=0400 AND termID=2000 AND SchoolID=5)
0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 34967337
Please consider error handling also.
If the value of AES_19 is Zero, then we will have "Divide by zero error encountered" error.
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 34968987
Handling the potential zero denominator (good catch!)...



DECLARE @numerator decimal(16, 4), @demoninator decimal(16, 4)

SET @numerator = 
    (select COUNT(*) 
    from students
    where grade_level=4 AND Enroll_Status=0 AND SchoolID=5 AND home_room<>'SESO')

SET @denominator = 
    (select COUNT(*) 
    from Sections
    where course_number=0400 AND termID=2000 AND SchoolID=5)

--If you want the result to be NULL:

SELECT CASE WHEN @denominator <> 0 THEN  @numerator / @denominator ELSE NULL END AS Result

--If you want the result to be zero:

SELECT CASE WHEN @denominator <> 0 THEN  @numerator / @denominator ELSE 0 END AS Result

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36119873
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

696 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