Informix SQL構文-ネストカウント、合計、ラウンド
質問
この質問の単純さについて事前に謝罪させていただきます(ジェフのポッドキャストと、質問の質が「馬鹿げている」という彼の懸念を聞いた)、私は立ち往生している。私は、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"であることを述べておかなければなりません。および「終了」。これらの結果を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ステートメントが存在しない場合、「終了」をカウントすると「生徒」をカウントした場合とまったく同じ結果が生成されます。
--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:
「countished(finished)/ count(*)」が1を返さない唯一の理由は、「finished」がnullを受け入れる場合だけであるため、「finished」がnullを使用できるようにします。スコア23の7行を入力して、小数点以下の桁数を増やしました(そして、スコア43の1行を変更して、小数点以下の桁数が多い2番目の数値を生成しました)。