Frage

Lassen Sie mich schonmal im Voraus für die Einfachheit dieser Frage (ich hörte Jeff mit seinem podcast und seiner Sorge, dass die Qualität der Fragen "verdummt"), aber ich bin stecken.Ich bin mit AquaData zu schlagen, meine Informix DB.Es gibt schrulligen kleinen Nuancen zwischen MS SQL und Informix SQL.Wie auch immer, ich bin versucht zu tun, eine einfache verschachtelten Ausdruck und er hasst mich.

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

Die Zeile mit der einfachen Unterteilung Ausdruck gibt den Prozentsatz der Leute, die fertig sind, die genau das, was ich will...ich brauche nur das Ergebnis gerundet auf 2 stellen.Die auskommentierte Zeile (--) funktioniert nicht.Ich habe versucht, jede Variante kann ich möglicherweise denken.

*Ich bin NICHT versuchen, zu verwenden die Linien 5 & 6 gleichzeitig


Tut mir Leid, ich sollte erwähnt haben, dass now_calc ist eine temp-Tabelle und den Feldnamen sind eigentlich "Schüler" und "fertig".Hatte ich Sie genannt, denn ich werde spucken werden diese Ergebnisse direkt in Excel und ich wollte, dass die Feldnamen doppelklicken, als Spaltenüberschriften.Also, ich verstehe, was Sie sagen, und auf der Grundlage, dass, ich habe es funktioniert, indem das entfernen der (*) wie folgt:

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

Ich bin auch die gesamte Abfrage - macht es mehr Sinn, um jemand anderes suchen, auf dieser.Aus einer Lernens, es ist wichtig zu beachten, dass der einzige Grund, count Werke auf die 'fertig' - Feld ist, weil der Case-Anweisung, der die Werte 1 oder null, je nach Auswertung der Case-Anweisung.Wenn das case-Anweisung nicht vorhanden ist, zählen "fertig" würden produzieren die genauen gleichen Ergebnisse wie die Zählung der 'Schüler'.

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

Vielen Dank!

War es hilfreich?

Lösung

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;

Beachten Sie, dass die Ausgabe-Spalte namens "Studenten" wurde wiederholt und war auch verwirrt.Der WIE ich ist optional.

Ich habe nun auch formell validiert die syntax vor-IDS, und es ist verwendbar:

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:

Ich lass 'fertig' machen, null-Werte, weil der einzige Grund für 'count(fertig) / count (*)" nicht zur Rückgabe 1 ist, wenn 'fertig' null-Werte akzeptiert-nicht sehr gute Tabelle design, though.Und ich lege 7 Zeilen mit Partitur 23 um eine große Anzahl von Dezimalstellen (und dann verändert eine Zeile der Gäste mit 43 zum erzeugen einer zweiten Nummer, mit einer großen Anzahl von Nachkommastellen).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top