【问题标题】:I want help in 2 queries : a)How many students have graduated with first class? b)How many students have obtained distinction?我需要 2 个问题的帮助:a) 有多少学生以一流的成绩毕业? b) 有多少学生获得了荣誉?
【发布时间】:2022-01-24 15:44:55
【问题描述】:

根据学生获得的分数对学生进行评分如下:

40-50  - Second class
50-60  - First Class
60-80  - First Class
80-100 - Distinctions.

上述查询的 Stud 表如下:

ID|Name     |Marks                                                                                                                                                                           
11|Britney  | 95    
12|Dyana    | 55    
13|Jenny    | 66    
14|Christene| 88    
15|Meera    | 24    
16|Priya    | 76    
17|Priyanka | 77    
18|Paige    | 74    
19|Samantha | 87    
21|Julia    | 96    
27|Evil     | 79    
29|Jane     | 64    
31|Scarlet  | 80    
32|Kristeen |100    
34|Fanny    | 75    
37|Belvet   | 78
38|Danny    | 75

我尝试创建成绩表,首先使用以下查询将成绩分配给表:

Select from stud Grade=Case 
when marks>100 then 'Distinction' 
when 80>70 and marks<100 then 'Distinction' 
when marks>60 and marks<80 then 'First Class' 
when marks>50 and marks<60 then 'First Class' 
when marks>40 and marks<50 then 'Second Class' 
when marks<40 then 'Fail' 
else 'No Grade Available' end Grade ; 

【问题讨论】:

  • 从 stud Grade=Case 当标记>100 时选择,然后当 80>70 时标记为“Distinction”,标记60 时选择“Distinction”,标记 50 和标记40 和标记
  • 我已在评论部分添加了我尝试过的查询,因为我无法在主体中添加代码。出现重复错误
  • 请编辑问题以添加内容,而不是添加到 cmets。您可以随时编辑。
  • 你的逻辑不允许分数落在边界上。

标签: sql mysql-workbench


【解决方案1】:
Select count(*) res from stud where marks between 80 and 100

结果将有“res”,其中学生的分数在 80 到 100 之间

【讨论】:

  • 用不同的运算符或标记修改查询以获得不同的结果。希望对您有所帮助。
【解决方案2】:

当标记值与要分配给相关组的条件匹配时,我会使用计数器总和为 1,使用 CASE WHEN 表达式:

WITH
-- your input, don't use in real query ..
indata(ID,Name,Marks) AS (
          SELECT 11,'Britney',95
UNION ALL SELECT 12,'Dyana',55
UNION ALL SELECT 13,'Jenny',66
UNION ALL SELECT 14,'Christene',88
UNION ALL SELECT 15,'Meera',24
UNION ALL SELECT 16,'Priya',76
UNION ALL SELECT 17,'Priyanka',77
UNION ALL SELECT 18,'Paige',74
UNION ALL SELECT 19,'Samantha',87
UNION ALL SELECT 21,'Julia',96
UNION ALL SELECT 27,'Evil',79
UNION ALL SELECT 29,'Jane',64
UNION ALL SELECT 31,'Scarlet',80
UNION ALL SELECT 32,'Kristeen',100
UNION ALL SELECT 34,'Fanny',75
UNION ALL SELECT 37,'Belvet',78
UNION ALL SELECT 38,'Danny',75
)
-- real query starts here
SELECT
  SUM(CASE WHEN marks > 50 AND marks <= 80 THEN 1 END) AS first_class_count
, SUM(CASE WHEN marks > 80                 THEN 1 END) AS distinction_count
FROM indata
-- out  first_class_count | distinction_count 
-- out -------------------+-------------------
-- out                 11 |                 5

【讨论】:

    【解决方案3】:

    您可以在 select 语句中定义类别并在 group by 中使用它以获得更易于阅读的结果。此外,请确保正确定义边界。这是一个例子

    SELECT CASE WHEN marks BETWEEN 81 AND 100 then 'Distinction' 
                WHEN marks BETWEEN 51 AND 80 then 'First Class' 
                WHEN marks BETWEEN 40 AND 50 then 'Second class'  
                ELSE 'No Grade Available' 
            END Grade,
            COUNT(*) AS stud_count
    FROM stud 
    WHERE marks > 50   
    GROUP BY CASE WHEN marks BETWEEN 81 AND 100 then 'Distinction' 
                WHEN marks BETWEEN 51 AND 80 then 'First Class' 
                WHEN marks BETWEEN 40 AND 50 then 'Second class'  
                ELSE 'No Grade Available' 
            END
    

    SQL Fiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2017-06-10
      相关资源
      最近更新 更多