4

我看过其他类似的问题,它们似乎与我尝试做的有点不同。

我有一个查询,我在其中选择两个计数值,并且我想将一个除以另一个。

这是我的工作代码:

SELECT (SELECT count(DISTINCT s.lastfirst) 
          FROM students s
          JOIN cc ON s.id = cc.studentid
          JOIN courses c on cc.course_number = c.course_number
         WHERE cc.schoolid='109'
           AND c.course_name LIKE 'AP %' 
           AND substr(cc.termid,0,1) <> '-'
           AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY') AND  
               to_date('08/01/2011','MM/DD/YYYY')) as AP, 
       (SELECT count(DISTINCT s.lastfirst) 
          FROM students s
          JOIN cc ON s.id = cc.studentid
          JOIN courses c on cc.course_number = c.course_number
         WHERE cc.schoolid = '109'
           AND substr(cc.termid,0,1) <> '-'
           AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY') 
           AND to_date('08/01/2011','MM/DD/YYYY')) as ttl
 FROM DUAL

这是我打破它的地方:

SELECT (SELECT count(DISTINCT s.lastfirst) 
          FROM students s
          JOIN cc ON s.id = cc.studentid
          JOIN courses c on cc.course_number = c.course_number
         WHERE cc.schoolid='109'
           AND c.course_name LIKE 'AP %' 
           AND substr(cc.termid,0,1) <> '-'
           AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY') 
           AND to_date('08/01/2011','MM/DD/YYYY')) as AP, 
       (SELECT count(DISTINCT s.lastfirst) 
          FROM students s
          JOIN cc ON s.id = cc.studentid
          JOIN courses c on cc.course_number = c.course_number
         WHERE cc.schoolid = '109'
           AND substr(cc.termid,0,1) <> '-'
           AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY')
           AND to_date('08/01/2011','MM/DD/YYYY')) as ttl,
      (AP / ttl) as pcnt
 FROM DUAL

有人可以告诉我这样做的正确方法吗?

谢谢你

4

1 回答 1

7

最简单的答案就是将两个值相除:

SELECT (SELECT count(DISTINCT s.lastfirst) 

FROM students s
JOIN cc ON s.id = cc.studentid
JOIN courses c on cc.course_number = c.course_number    
WHERE cc.schoolid='109'    
AND c.course_name LIKE 'AP %'     
AND substr(cc.termid,0,1) <> '-'    
AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY') 
    AND to_date('08/01/2011','MM/DD/YYYY')) /    
(SELECT count(DISTINCT s.lastfirst)     
FROM students s
JOIN cc ON s.id = cc.studentid
JOIN courses c on cc.course_number = c.course_number       
WHERE cc.schoolid = '109'    
AND substr(cc.termid,0,1) <> '-'    
AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY') 
    AND to_date('08/01/2011','MM/DD/YYYY')) as quotient

FROM DUAL

如果您想保留这些值并获得商,您可以这样做:

SELECT AP, ttl, AP/ttl AS quotient
FROM (
SELECT (SELECT count(DISTINCT s.lastfirst) 
          FROM students s
          JOIN cc ON s.id = cc.studentid
          JOIN courses c on cc.course_number = c.course_number
         WHERE cc.schoolid='109'
           AND c.course_name LIKE 'AP %' 
           AND substr(cc.termid,0,1) <> '-'
           AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY') AND  
               to_date('08/01/2011','MM/DD/YYYY')) as AP, 
       (SELECT count(DISTINCT s.lastfirst) 
          FROM students s
          JOIN cc ON s.id = cc.studentid
          JOIN courses c on cc.course_number = c.course_number
         WHERE cc.schoolid = '109'
           AND substr(cc.termid,0,1) <> '-'
           AND cc.dateenrolled BETWEEN to_date('08/01/2010','MM/DD/YYYY') 
           AND to_date('08/01/2011','MM/DD/YYYY')) as ttl
 FROM DUAL)

但是此时您也可以只在调用代码而不是 SQL 中进行除法,因为无论如何您只是选择了一个具有两个标量值的结果行。

于 2012-11-05T17:27:02.793 回答