美文网首页
MsSql 自定义分数段,按分数段统计考试人次

MsSql 自定义分数段,按分数段统计考试人次

作者: 果子_先生 | 来源:发表于2019-01-09 11:07 被阅读8次
    --分数段分布 
    DECLARE @levels VARCHAR(100) = '10,20,30,40,50,60,70,80,90,100'; --自定义分数段
    DECLARE @paperId VARCHAR(100)='0000000000001019'--试卷编号
    WITH tbTemp AS (--处理分数段
             SELECT L.level
             FROM   (
                        SELECT [value] = CONVERT(XML, '<v>' + REPLACE(@levels, ',', '</v><v>') + '</v>')
                    ) T
                    OUTER APPLY (
                 SELECT LEVEL = CONVERT(INT, N.v.value('.', 'varchar(100)'))
                 FROM   T.[value].nodes('/v') N(v)
             ) L
         ), 
         tbLevel AS (--生成分数段区间
             SELECT (
                        SELECT ISNULL(MAX(t.level), 0)
                        FROM   tbTemp AS t
                        WHERE  t.LEVEL < tt.level
                    )         AS level0,
                    tt.level  AS level1
             FROM   tbTemp    AS tt
         ),
         tbScore AS(--处理成绩为null或0分,为了分配到0-?区间
             SELECT (CASE WHEN tp.GetScore > 0 THEN tp.GetScore ELSE 0.1 END) AS 
                    pScore
             FROM   成绩表 AS tp
             WHERE  tp.PaperID = @paperId
         )
    --统计区间人次
    SELECT tl.level0,
           tl.level1,
           (
               SELECT COUNT(0)
               FROM   tbScore AS ts
               WHERE  ts.pScore > level0
                      AND ts.pScore <= level1
           )        AS ExamCount
    FROM   tbLevel  AS tl;
    
    

    levels = '10,20,30,40,50,60,70,80,90,100';

    1.png

    levels = '40,60,80,100';

    2.png

    相关文章

      网友评论

          本文标题:MsSql 自定义分数段,按分数段统计考试人次

          本文链接:https://www.haomeiwen.com/subject/dluwrqtx.html