2008-10-02 30 views
2

爲了簡化這個問題,我要提前道歉(我聽說傑夫的播客和他擔心問題的質量會「下降」),但我是卡住。我正在使用AquaData來打我的Informix數據庫。 MS SQL和Informix 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位結果的百分比。註釋行( - )不起作用。我試過了我可能想到的每一個變化。

*我並不想在


我很抱歉,我應該提到,now_calc是一個臨時表,同時用線5 & 6和字段名實際上是「學生「和」完成「。我讓他們這樣命名,因爲我要將這些結果直接吐到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語句使值爲1或null,這取決於對Case語句的評估。如果不存在該案例陳述,則計數「完成」將產生與計算「學生」完全相同的結果。

--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 

謝謝!

回答

2
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; 

請注意,輸出列名稱'學生'正在重複,也令您感到困惑。我使用的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: 

我讓「完成」拿空,因爲唯一的理由「計數(完)/ COUNT(*)」如果'完成'接受空值 - 不是很好的表設計,但不返回1。而且我把7分的23分數得到大量的小數位(然後用43分改變了一行,產生了第二個有大量小數位的數字)。