Informix SQL语法-嵌套计数,总和,舍入
时间:2020-03-06 15:02:45 来源:igfitidea点击:
让我提前为这个问题的简单性道歉(我听到Jeff的播客,以及他担心问题的质量将被"降低"),但我被困住了。我正在使用AquaData打我的Informix数据库。 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
我将整个查询包括在内,这对于其他任何人都可能更有意义。从学习的角度来看,重要的是要注意在" finished"字段上进行计数的唯一原因是因为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
谢谢!
解决方案
SELECT score, count(*) students, count(finished) finished, count(finished) / count(*) AS something_other_than_students, round((count(finished) / count(*)),2) AS rounded_value FROM now_calc GROUP BY score ORDER BY score;
请注意,输出列名称" students"被重复使用,这也使我们感到困惑。我使用的AS是可选的。
我现在已经针对IDS正式验证了语法,并且可以使用:
Black JL: sqlcmd -Ffixsep -d stores -xf xx.sql | sed 's/ //g' + create temp table now_calc(finished CHAR(1), score INTEGER, name CHAR(10) PRIMARY KEY); + insert into now_calc values(null, 23, 'a'); + insert into now_calc values('y', 23, 'b'); + insert into now_calc values('y', 23, 'h'); + insert into now_calc values('y', 23, 'i'); + insert into now_calc values('y', 23, 'j'); + insert into now_calc values('y', 43, 'c'); + insert into now_calc values(null, 23, 'd'); + insert into now_calc values('y', 43, 'e'); + insert into now_calc values(null, 23, 'f'); + insert into now_calc values(null, 43, 'g'); + SELECT score, count(*) students, count(finished) finished, count(finished) / count(*) AS something_other_than_students, round((count(finished) / count(*)),2) AS rounded_value FROM now_calc GROUP BY score ORDER BY score; 23| 7| 4| 5.71428571428571E-01| 0.57 43| 3| 2| 6.66666666666667E-01| 0.67 Black JL:
我让'finished'取空值是因为'count(finished)/ count(*)'不返回1的唯一原因是'finished'接受nulls-虽然不是很好的表设计。然后我将得分为23的7行放入小数位数(然后将得分为43的一行更改为第二个具有小数位数的数字)。