这个问题 困扰一天了,还是没想到解决办法来向大家请教一下,表我已经简化了很多很多
预想结果
之前的打算是如果查选项的百分比的话    但是题目一多就完蛋了人 说我是  行转列的问题
之前没有遇到过,过来请教一下

解决方案 »

  1.   

    在Oracle 自己写了一个比较笨的算法:
    with t1 as (
    select cid,
    sum(case when score =2 then 1 else 0 end) g1,     --得2分的人
    sum(case when score =1 then 1 else 0 end) g2,    --得1分的人
    sum(score) score
    from stu_grades_test where tid='t1' group by cid  ---学生科目表
    ),t2 as (
    select cid,
    sum(case when score =2 then 1 else 0 end) g1,     --得2分的人
    sum(case when score =1 then 1 else 0 end) g2,    --得1分的人
    sum(score) score
    from stu_grades_test where tid='t2' group by cid
    ), gd as (
    select 
    cid,           
    sum(score) score ,           --每个科目的总分
    count(distinct userid) u_id  --每个科目的学生个数 
    from stu_grades_test group by cid                  --学生成绩表
    )
    select 
    gd.cid,  --课程名称
    oo.ctype,--课程状态
    gd.score,--总分
    t1.g1/gd.u_id, --得2分的人
    t1.g2/gd.u_id, --得1分的人
    t1.score,      --此题总分
    t2.g1/gd.u_id, --得2分的人
    t2.g2/gd.u_id, --得1分的人
    t2.score       --此题总分
    from  gd 
    left join t1 on t1.cid = gd.cid
    left join t2 on t2.cid = gd.cid
    left join stu_object_test oo on oo.cid =  gd.cid