2008-10-02 13 views
2

이 질문의 단순성에 대해 미리 사과 드리겠습니다. (제프의 팟 캐스트와 질문의 질이 "어리석게"될 것이라는 우려를 듣고) 붙어있어. AquaData를 사용하여 Informix DB에 충돌합니다. MS SQL과 Informix SQL 사이에는 약간의 뉘앙스가 있습니다. 어쨌든, 간단한 중첩 된 표현을하려고하는데 그것은 나를 싫어합니다.Informix SQL 구문 - 둥지 수, 합계, Round

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 

간단한 분할 식 선은 정확히 내가 원하는 ... 난 그냥이 곳으로 반올림 결과를 필요로 완성 사람들의 비율을 반환합니다. 주석 처리 된 행 (-)이 작동하지 않습니다. 나는 내가 생각할 수있는 모든 변이를 ​​시도했다.

* 내가


가 미안 해요, 내가 임시 테이블 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 문 때문입니다. 그 case 문이 존재하지 않는다면 'finished'로 계산하면 'students'로 계산하는 것과 똑같은 결과가 산출됩니다.

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

출력 열 이름 '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: 

'완료'널 (null)을 가지고 있기 때문에 유일한 이유는 '수 (완료)/COUNT (*)' 1을 반환하지 않는 것은 '완료'가 nulls를 허용하는지 아닌지 - 아주 좋은 테이블 디자인은 아닙니다. 그리고 나는 소수점이 많은 곳을 얻기 위해 스코어 23과 함께 7 개의 행을 썼다. (소수점이 많은 두 번째 숫자를 생성하기 위해 스코어 43으로 한 행을 변경했다.)