计算特定范围内行数的百分比

时间:2012-12-15 06:37:05

标签: mysql sql mysqli

我有一个学生姓名和标记列表存储在如下表格中。

CREATE TABLE StudentsList(StudentId INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
                              Name  VARCHAR(50),
                              Marks INT);


INSERT INTO StudentsList(Name, Marks)
      VALUES('Student A', 20),
            ('Student B', 45),
            ('Student C', 90),
            ('Student D', 81),
            ('Student E', 50),
            ('Student F', 10),
            ('Student G', 85),
            ('Student H', 41),
            ('Student I', 66),
            ('Student J', 65),
            ('Student K', 05),
            ('Student L', 20),
            ('Student M', 19),
            ('Student N', 80),
            ('Student O', 90),
            ('Student P', 91),
            ('Student Q', 10),
            ('Student R', 29);

我想根据他们贡献的标记范围和百分比对学生进行分组。

    MarkRange   NoOfStudents  Percentage
      0 - 20       4             22.22
     20 - 50       5             27.77  
     50 - 70       3             16.66 
     70 - 90       3             16.66  
     90            3             16.66 

我尝试了下面的查询并为0-20岁之间的学生带来了结果

SELECT COUNT(*) , COUNT(*)/(T.total)* 100
  FROM StudentsList,
       (SELECT COUNT(*) AS total 
          FROM StudentsList) AS T 
         WHERE Marks >= 0 and  Marks < 20

如何使用单个查询

来完成此操作

3 个答案:

答案 0 :(得分:1)

    SELECT COUNT(*) , COUNT(*)/(T.total)* 100, '0 - 20' AS Range
      FROM StudentsList,
           (SELECT COUNT(*) AS total 
              FROM StudentsList) AS T 
             WHERE Marks >= 0 and  Marks < 20
    UNION 
    SELECT COUNT(*) , COUNT(*)/(T.total)* 100, '20 - 50' AS Range
      FROM StudentsList,
           (SELECT COUNT(*) AS total 
              FROM StudentsList) AS T 
             WHERE Marks >= 20 and  Marks < 50
    UNION 
    SELECT COUNT(*) , COUNT(*)/(T.total)* 100, '50 - 70' AS Range
      FROM StudentsList,
           (SELECT COUNT(*) AS total 
              FROM StudentsList) AS T 
             WHERE Marks >= 50 and  Marks < 70
    UNION 
    SELECT COUNT(*) , COUNT(*)/(T.total)* 100, '70 - 90' AS Range
      FROM StudentsList,
           (SELECT COUNT(*) AS total 
              FROM StudentsList) AS T 
             WHERE Marks >= 70 and  Marks < 90
   UNION 
   SELECT COUNT(*) , COUNT(*)/(T.total)* 100, '90 Above' AS Range
     FROM StudentsList,
          (SELECT COUNT(*) AS total 
             FROM StudentsList) AS T 
            WHERE Marks >= 90

我找到了使用联盟的答案。

如果有其他解决方案,请建议我

答案 1 :(得分:1)

试试这个:

SELECT CONCAT(A.minRange, ' - ', A.maxRange) MarkRange, COUNT(sl.Name) NoOfStudents, 
    (SELECT COUNT(sl.Name) / COUNT(*) * 100 FROM StudentsList) Percentage
FROM studentslist sl 
INNER JOIN (SELECT 1 id, 0 minRange, 20 maxRange 
    UNION 
    SELECT 2 id, 20 minRange, 50 maxRange 
    UNION 
    SELECT 3 id, 50 minRange, 70 maxRange 
    UNION 
    SELECT 4 id, 70 minRange, 90 maxRange 
    UNION 
    SELECT 5 id, 90 minRange, 100 maxRange 
    ) AS A ON sl.Marks >= A.minRange AND sl.Marks < A.maxRange 
GROUP BY A.id;

答案 2 :(得分:0)

试试这个:

SELECT COUNT(*) as 'number of students', COUNT(*)/(T.total)* 100 as 'Percentage', '0 - 20' AS 'Range' 
  FROM StudentsList,
       (SELECT COUNT(*) AS total 
          FROM StudentsList) AS T 
         WHERE Marks >= 0 and  Marks < 20
UNION
SELECT COUNT(*) , COUNT(*)/(T.total)* 100  as 'Percentage', '20 - 50' AS 'Range'
  FROM StudentsList,
       (SELECT COUNT(*) AS total 
          FROM StudentsList) AS T 
         WHERE Marks >= 0 and  Marks >= 20 and Marks < 50
UNION
SELECT COUNT(*) , COUNT(*)/(T.total)* 100 as 'Percentage', '50 - 70' AS 'Range'
  FROM StudentsList,
       (SELECT COUNT(*) AS total 
          FROM StudentsList) AS T 
         WHERE Marks >= 0 and  Marks >= 70 and Marks < 90
UNION
SELECT COUNT(*) , COUNT(*)/(T.total)* 100 as 'Percentage', '70 - 90' AS 'Range'
  FROM StudentsList,
       (SELECT COUNT(*) AS total 
          FROM StudentsList) AS T 
         WHERE Marks >= 0 and  Marks >= 70 and Marks < 90
UNION
SELECT COUNT(*) , COUNT(*)/(T.total)* 100 as 'Percentage', 'Above 90' AS 'Range'
  FROM StudentsList,
       (SELECT COUNT(*) AS total 
          FROM StudentsList) AS T 
         WHERE Marks >= 0 and  Marks >= 90 
order by 3

输出:

NUMBER OF STUDENTS  PERCENTAGE      RANGE
4                   22.2222         0 - 20
5                   27.7778         20 - 50
3                   16.6667         50 - 70
3                   16.6667         70 - 90
3                   16.6667         Above 90

您可以看到它的运行方式 here