我看过其他类似的问题,它们似乎与我尝试做的有点不同。
我有一个查询,我在其中选择两个计数值,并且我想将一个除以另一个。
这是我的工作代码:
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
有人可以告诉我这样做的正确方法吗?
谢谢你