2017-06-03 44 views
1

我想繪製一個面積圖的學生每積累分數沒有學生在一個軸上和累積百分比的標記在其他。 我嘗試下面的代碼,並且需要幫助查找%頻率:如何獲得跑步總數和跑步總數的一組範圍的頻率百分比

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 [email protected]_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 
+0

fyi:'10-20'是一個整數,''10-20''是一個字符串。 – HABO

+0

mybad..but仍然不能解決問題 –

+0

請提供您試圖實現的輸出樣本。 –

回答

1

好的; 2008 ** SP代碼更新**;

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 [email protected]_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; 
+0

這應該代替行代碼; WITH累積AS在我的代碼? –

+0

它給出無效的列名C –

+0

只需複製並粘貼,不要做任何事情。它適用於我 – JPF

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 [email protected]_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 
    ) 
+0

謝謝,但需要代碼爲2008-R2 –