让我为这个问题的简单性提前道歉(我听到了 Jeff 的播客以及他担心问题的质量会被“降低”),但我被卡住了。我正在使用 AquaData 来访问我的 Informix DB。MS SQL 和 Informix SQL 之间有一些古怪的细微差别。无论如何,我正在尝试做一个简单的嵌套表达式,它讨厌我。
select
score,
count(*) students,
count(finished) finished,
count(finished) / count(*)students
-- round((count(finished) / count(*)students),2)
from now_calc
group by score
order by score
带有简单除法表达式的行返回完成的人的百分比,这正是我想要的......我只需要将结果四舍五入到 2 位。注释行 (--) 不起作用。我已经尝试了所有我能想到的变化。
*我不想同时使用第 5 行和第 6 行
对不起,我应该提到 now_calc 是一个临时表,字段名称实际上是“students”和“finished”。我将它们命名为这样,因为我要将这些结果直接输出到 Excel 中,并且我希望字段名称兼作列标题。所以,我明白你在说什么,并基于此,我通过删除 (*) 使其工作,如下所示:
select
score,
count(students) students,
count(finished) finished,
round((count(finished) / count(students) * 100),2) perc
from now_calc
group by score
order by score
我包括了整个查询——这对其他任何人来说可能更有意义。从学习的角度来看,重要的是要注意计数在“已完成”字段上起作用的唯一原因是因为 Case 语句根据对 Case 语句的评估将值设为 1 或 null。如果该案例陈述不存在,计算“完成”将产生与计算“学生”完全相同的结果。
--count of cohort members and total count of all students (for reference)
select
cohort_yr,
count (*) id,
(select count (*) id from prog_enr_rec where cohort_yr is not null and prog = 'UNDG' and cohort_yr >=1998) grand
from prog_enr_rec
where cohort_yr is not null
and prog = 'UNDG'
and cohort_yr >=1998
group by cohort_yr
order by cohort_yr;
--cohort members from all years for population
select
id,
cohort_yr,
cl,
enr_date,
prog
from prog_enr_rec
where cohort_yr is not null
and prog = 'UNDG'
and cohort_yr >=1998
order by cohort_yr
into temp pop with no log;
--which in population are still attending (726)
select
pop.id,
'Y' fin
from pop, stu_acad_rec
where pop.id = stu_acad_rec.id
and pop.prog = stu_acad_rec.prog
and sess = 'FA'
and yr = 2008
and reg_hrs > 0
and stu_acad_rec.cl[1,1] <> 'P'
into temp att with no log;
--which in population graduated with either A or B deg (702)
select
pop.id,
'Y' fin
from pop, ed_rec
where pop.id = ed_rec.id
and pop.prog = ed_rec.prog
and ed_rec.sch_id = 10
and (ed_rec.deg_earn[1,1] = 'B'
or (ed_rec.deg_earn[1,1] = 'A'
and pop.id not in (select pop.id
from pop, ed_rec
where pop.id = ed_rec.id
and pop.prog = ed_rec.prog
and ed_rec.deg_earn[1,1] = 'B'
and ed_rec.sch_id = 10)))
into temp grad with no log;
--combine all those that either graduated or are still attending
select * from att
union
select * from grad
into temp all_fin with no log;
--ACT scores for all students in population who have a score (inner join to eliminate null values)
--score > 50 eliminates people who have data entry errors - SAT scores in ACT field
--2270
select
pop.id,
max (exam_rec.score5) score
from pop, exam_rec
where pop.id = exam_rec.id
and ctgry = 'ACT'
and score5 > 0
and score5 < 50
group by pop.id
into temp pop_score with no log;
select
pop.id students,
Case when all_fin.fin = 'Y' then 1 else null end finished,
pop_score.score
from pop, pop_score, outer all_fin
where pop.id = all_fin.id
and pop.id = pop_score.id
into temp now_calc with no log;
select
score,
count(students) students,
count(finished) finished,
round((count(finished) / count(students) * 100),2) perc
from now_calc
group by score
order by score
谢谢!