1

以下のようなテーブルに保存されている学生の名前とマークのリストがあります。

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

単一のクエリを使用してこれを行うにはどうすればよいですか

4

3 に答える 3

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

ユニオンを使用して回答を見つけました。

他の解決策がある場合は、親切に私に提案してください

于 2012-12-15T06:46:37.763 に答える
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;
于 2012-12-15T06:57:23.703 に答える
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

走り方はこちらからご覧いただけます

于 2012-12-15T06:44:03.440 に答える