如何从一组范围

时间:2017-06-03 07:00:51

标签: asp.net tsql charts sql-server-2008-r2

我想绘制学生每个累积分数的区域图,其中一个轴上没有学生,另一个轴上的分数累积百分比。 我尝试了以下代码,需要帮助才能找到%freq:

    USE [newexam2017]
    GO
    StoredProcedure [dbo].[sp_range]    Script Date: 06/03/2017 10:50:43 
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[sp_range]
    @sub_code nvarchar(10)
    AS
    BEGIN
    SET NOCOUNT ON;

    CREATE TABLE #range
    (
    Interval varchar(10),
    NoOfStudents varchar(8)

    );

   INSERT INTO #range(Interval) values('10-20'),('21-30'),('31-40'),('41-
   50'),('51-60'),('61-70'),('71-80'),('81-90'),('91-100')
   SELECT t.Interval , ISNULL(count(t.Interval),0) as [NoOfStudents] 
   FROM (
   SELECT CASE  
   WHEN Total_marks BETWEEN 10 and 20 THEN '10-20'
   WHEN Total_marks BETWEEN 21 and 30 THEN '21-30'
   WHEN Total_marks BETWEEN 31 and 40 THEN '31-40'
   WHEN Total_marks BETWEEN 41 and 50 THEN '41-50'
   WHEN Total_marks BETWEEN 51 and 60 THEN '51-60'
   WHEN Total_marks BETWEEN 61 and 70 THEN '61-70'
   WHEN Total_marks BETWEEN 71 and 80 THEN '71-80'
   WHEN Total_marks BETWEEN 81 and 90 THEN '81-90'
   WHEN Total_marks BETWEEN 91 and 100 THEN '91-100'
   end as Interval
   from newexam2017_transctn where sub_code=@sub_code) t
   right join #range i on i.Interval = t.Interval 
   group by t.Interval

  ;WITH cummulative AS
  (
  SELECT Interval,NoOfStudents,RunningTotal= NoOfStudents
  FROM #range
  WHERE Interval = '10-20' 
  UNION ALL
  SELECT y.Interval, y.NoOfStudents, cummulative.RunningTotal + 
  y.NoOfStudents
  FROM cummulative INNER JOIN #range AS y
  ON y.NoOfStudents = cummulative.NoOfStudents + 1
  )
  SELECT Interval, NoOfStudents, RunningTotal 
  FROM cummulative
  ORDER BY Interval
  OPTION (MAXRECURSION 10000);

 DROP TABLE #range;
 END

但是我收到以下错误:

锚点与列

中的递归部分之间的类型不匹配

并且它仅显示2 3个范围,并且对于第一个范围10-20

为空

希望表格如下:

间隔|没有学生|总累计学生人数

 10-20  | 0              | 0
 20-30  | 3              | 3
 30-40  | 12             | 15
 40-50  | 11             | 26
 50-60  | 35             | 61
 60-70  | 15             | 76
 70-80  | 14             | 90
 80-90  | 6              | 96
 90-100 | 4              | 100

2 个答案:

答案 0 :(得分:1)

确定; 2008 ** SP CODE UPDATED **;

ALTER PROCEDURE [dbo].[sp_range]
@sub_code nvarchar(10)

AS

SET NOCOUNT ON;
;WITH data AS 
    ( 
    SELECT CASE  
       WHEN Total_marks BETWEEN 10 and 20 THEN '10-20'
       WHEN Total_marks BETWEEN 21 and 30 THEN '21-30'
       WHEN Total_marks BETWEEN 31 and 40 THEN '31-40'
       WHEN Total_marks BETWEEN 41 and 50 THEN '41-50'
       WHEN Total_marks BETWEEN 51 and 60 THEN '51-60'
       WHEN Total_marks BETWEEN 61 and 70 THEN '61-70'
       WHEN Total_marks BETWEEN 71 and 80 THEN '71-80'
       WHEN Total_marks BETWEEN 81 and 90 THEN '81-90'
       WHEN Total_marks BETWEEN 91 and 100 THEN '91-100'
       end as Interval,
       C=COUNT(1)
       from newexam2017_transctn 
       where sub_code=@sub_code
        GROUP BY 
            (
            CASE  
           WHEN Total_marks BETWEEN 10 and 20 THEN '10-20'
           WHEN Total_marks BETWEEN 21 and 30 THEN '21-30'
           WHEN Total_marks BETWEEN 31 and 40 THEN '31-40'
           WHEN Total_marks BETWEEN 41 and 50 THEN '41-50'
           WHEN Total_marks BETWEEN 51 and 60 THEN '51-60'
           WHEN Total_marks BETWEEN 61 and 70 THEN '61-70'
           WHEN Total_marks BETWEEN 71 and 80 THEN '71-80'
           WHEN Total_marks BETWEEN 81 and 90 THEN '81-90'
           WHEN Total_marks BETWEEN 91 and 100 THEN '91-100' end
           )
    )
    SELECT 
        d.Interval,
        d.c, 
        run = SUM(d2.c)
    FROM 
        data d
    INNER JOIN 
        data AS d2 ON d.Interval >= d2.Interval
    GROUP BY 
        d.Interval, 
        d.c
    ORDER BY 
        d.Interval;

答案 1 :(得分:0)

如果您使用的是SQL 2012及更高版本,请尝试以下操作;

SELECT CASE  
   WHEN Total_marks BETWEEN 10 and 20 THEN '10-20'
   WHEN Total_marks BETWEEN 21 and 30 THEN '21-30'
   WHEN Total_marks BETWEEN 31 and 40 THEN '31-40'
   WHEN Total_marks BETWEEN 41 and 50 THEN '41-50'
   WHEN Total_marks BETWEEN 51 and 60 THEN '51-60'
   WHEN Total_marks BETWEEN 61 and 70 THEN '61-70'
   WHEN Total_marks BETWEEN 71 and 80 THEN '71-80'
   WHEN Total_marks BETWEEN 81 and 90 THEN '81-90'
   WHEN Total_marks BETWEEN 91 and 100 THEN '91-100'
   end as Interval,
   COUNT(1),
   CountCumlative=SUM(COUNT(1)) OVER (ORDER BY newid() ROWS UNBOUNDED PRECEDING)
   from newexam2017_transctn 
  where sub_code=@sub_code
group by
    (
    CASE  
   WHEN Total_marks BETWEEN 10 and 20 THEN '10-20'
   WHEN Total_marks BETWEEN 21 and 30 THEN '21-30'
   WHEN Total_marks BETWEEN 31 and 40 THEN '31-40'
   WHEN Total_marks BETWEEN 41 and 50 THEN '41-50'
   WHEN Total_marks BETWEEN 51 and 60 THEN '51-60'
   WHEN Total_marks BETWEEN 61 and 70 THEN '61-70'
   WHEN Total_marks BETWEEN 71 and 80 THEN '71-80'
   WHEN Total_marks BETWEEN 81 and 90 THEN '81-90'
   WHEN Total_marks BETWEEN 91 and 100 THEN '91-100'
   end
    )