MYSQL计数和排序结果

时间:2014-04-01 16:49:42

标签: mysql

我已经对总计和计数结果编写了一个查询,并对这些结果执行了一些函数(计算百分比等)。我的查询有效,但我确信有更好的方法可以做到这一点。你会发现它是一个非常冗长的查询。我创建了一个将结果保存在数据库中的调查。

此查询按顺序对结果进行排序,然后总计每个问题的答案数量,然后根据这些结果分配给每个问题,然后根据为每个问题分配的点数创建百分比,然后给出了很高的百分比。

如前所述,此查询有效,但如果您有更好的方法,请告诉我。

select 
y.Course as Course,
y.ins as Instructor,
y.comp as Completed,
((y.q1_percent+y.q2_percent+y.q3_percent+y.q4_percent+y.q5_percent+y.q6_percent)/6) as 'Total Percent',
y.Q1_percent as Q1,
y.Q2_percent as Q2,
y.Q3_percent as Q3,
y.Q4_percent as Q4,
y.Q5_percent as Q5,
y.Q6_percent as Q6,
y.comp*5 as 'Avail Points',
y.Q1_pts as 'Q1 Points',
y.Q2_pts as 'Q2 Points',
y.Q3_pts as 'Q3 Points',
y.Q4_pts as 'Q4 Points',
y.Q5_pts as 'Q5 Points',
y.Q6_pts as 'Q6 Points'

from ( select

    z.course as Course,
z.ins as ins,
z.comp as comp,
    z.Avail_Points as Avail_Pts,
    z.Q1_rating as Q1_Pts,
    (z.Q1_rating / z.Avail_Points * 100) as Q1_Percent,
    z.Q2_rating as Q2_Pts,
    (z.Q2_rating / z.Avail_Points * 100) as Q2_Percent,
    z.Q3_rating as Q3_Pts,
    (z.Q3_rating / z.Avail_Points * 100) as Q3_Percent,
    z.Q4_rating as Q4_Pts,
    (z.Q4_rating / z.Avail_Points * 100) as Q4_Percent,
    z.Q5_rating as Q5_Pts,
    (z.Q5_rating / z.Avail_Points * 100) as Q5_Percent,
    z.Q6_rating as Q6_Pts,
    (z.Q6_rating / z.Avail_Points * 100) as Q6_Percent
from
    (select 
        x.course as course,
x.ins, x.comp,
            (x.comp * 5) as Avail_Points,
            ((x.q1_SD * 1) + (x.q1_D * 2) + (x.q1_NDA * 3) + (x.q1_A * 4) + (x.q1_SA * 5)) as q1_Rating,
            ((x.q2_SD * 1) + (x.q2_D * 2) + (x.q2_NDA * 3) + (x.q2_A * 4) + (x.q2_SA * 5)) as q2_Rating,
            ((x.q3_SD * 1) + (x.q3_D * 2) + (x.q3_NDA * 3) + (x.q3_A * 4) + (x.q3_SA * 5)) as q3_Rating,
            ((x.q4_SD * 1) + (x.q4_D * 2) + (x.q4_NDA * 3) + (x.q4_A * 4) + (x.q4_SA * 5)) as q4_Rating,
            ((x.q5_SD * 1) + (x.q5_D * 2) + (x.q5_NDA * 3) + (x.q5_A * 4) + (x.q5_SA * 5)) as q5_Rating,
            ((x.q6_SD * 1) + (x.q6_D * 2) + (x.q6_NDA * 3) + (x.q6_A * 4) + (x.q6_SA * 5)) as q6_Rating
    from
        (select 
        concat(c.course_num, ' - ', c.name) as Course,
            concat(i.fname, ' ', i.lname) as ins,
            count(a.pk_ansID) as 'comp',
            sum(a.q1 = 0) + sum(a.q2 = 0) + sum(a.q3 = 0) + sum(a.q4 = 0) + sum(a.q5 = 0) + sum(a.q6 = 0) as NR,
            sum(a.q1 = 1) + sum(a.q2 = 1) + sum(a.q3 = 1) + sum(a.q4 = 1) + sum(a.q5 = 1) + sum(a.q6 = 1) as SD,
            sum(a.q1 = 2) + sum(a.q2 = 2) + sum(a.q3 = 2) + sum(a.q4 = 2) + sum(a.q5 = 2) + sum(a.q6 = 2) as D,
            sum(a.q1 = 3) + sum(a.q2 = 3) + sum(a.q3 = 3) + sum(a.q4 = 3) + sum(a.q5 = 3) + sum(a.q6 = 3) as NDA,
            sum(a.q1 = 4) + sum(a.q2 = 4) + sum(a.q3 = 4) + sum(a.q4 = 4) + sum(a.q5 = 4) + sum(a.q6 = 4) as A,
            sum(a.q1 = 5) + sum(a.q2 = 5) + sum(a.q3 = 5) + sum(a.q4 = 5) + sum(a.q5 = 5) + sum(a.q6 = 5) as SA,
            sum(a.q1 = 0) as q1_NR,
            sum(a.q2 = 0) as q2_NR,
            sum(a.q3 = 0) as q3_NR,
            sum(a.q4 = 0) as q4_NR,
            sum(a.q5 = 0) as q5_NR,
            sum(a.q6 = 0) as q6_NR,
            sum(a.q1 = 1) as q1_SD,
            sum(a.q2 = 1) as q2_SD,
            sum(a.q3 = 1) as q3_SD,
            sum(a.q4 = 1) as q4_SD,
            sum(a.q5 = 1) as q5_SD,
            sum(a.q6 = 1) as q6_SD,
            sum(a.q1 = 2) as q1_D,
            sum(a.q2 = 2) as q2_D,
            sum(a.q3 = 2) as q3_D,
            sum(a.q4 = 2) as q4_D,
            sum(a.q5 = 2) as q5_D,
            sum(a.q6 = 2) as q6_D,
            sum(a.q1 = 3) as q1_NDA,
            sum(a.q2 = 3) as q2_NDA,
            sum(a.q3 = 3) as q3_NDA,
            sum(a.q4 = 3) as q4_NDA,
            sum(a.q5 = 3) as q5_NDA,
            sum(a.q6 = 3) as q6_NDA,
            sum(a.q1 = 4) as q1_A,
            sum(a.q2 = 4) as q2_A,
            sum(a.q3 = 4) as q3_A,
            sum(a.q4 = 4) as q4_A,
            sum(a.q5 = 4) as q5_A,
            sum(a.q6 = 4) as q6_A,
            sum(a.q1 = 5) as q1_SA,
            sum(a.q2 = 5) as q2_SA,
            sum(a.q3 = 5) as q3_SA,
            sum(a.q4 = 5) as q4_SA,
            sum(a.q5 = 5) as q5_SA,
            sum(a.q6 = 5) as q6_SA
    from
        survey_answers a
    left outer join course c ON c.pk_courseID = a.fk_courseID
    left outer join instructor i ON (i.pk_insID = c.fk_insID)
    group by a.fk_courseID) x) z) y

0 个答案:

没有答案