给这个写一个查询..?

时间:2016-08-07 17:08:55

标签: mysql sql sql-server oracle

学生和成绩。学生包含三列ID,名称和标记,成绩 - (成绩,min_mark,max_mark)

Ketty为Eve提供了一个生成包含三列的报告的任务:名称,等级和标记。 Ketty不希望收到等级低于8的学生的NAMES。报告必须按年级降序排列 - 即先输入更高的成绩。如果分配了相同年级(1-10)的学生不止一个,请按字母顺序按名称命名这些学生。最后,如果等级低于8,请使用“NULL”作为名称,并按标记按升序列出。

写一个查询来帮助夏娃。

请注意 如果成绩小于8,则打印“NULL”作为名称。

我写了这个查询:

select name,grade,marks from students s join grades g on marks between min_mark and max_mark
where grade>7
order by 2 desc,name

select translate(name,name,'NULL'),grade,marks from students s join grades g on marks between min_mark and max_mark
where grade<=7
order by 3 desc

但不知道如何加入这两个查询。请帮忙。谢谢你

10 个答案:

答案 0 :(得分:1)

可能是这样的:

SELECT CASE WHEN g.grade < 8 THEN NULL ELSE s.name END as name,
    g.grade, s.marks 
FROM students AS s 
INNER JOIN grades AS g 
ON s.marks BETWEEN g.min_mark AND g.max_mark
ORDER BY g.grade DESC, s.name;

答案 1 :(得分:1)

在hackerrank上经过验证的代码...

select s.name, g.grade, s.marks from students s, grades g where g.grade>=8 and s.marks between g.min_mark and g.max_mark order by g.grade desc, s.name;

select 'NULL', g.grade, s.marks from students s, grades g where g.grade<8 and s.marks between g.min_mark and g.max_mark order by g.grade desc, s.marks;

enter image description here

答案 2 :(得分:0)

SELECT CASE WHEN Grades.Grade < 8 THEN NULL ELSE Students.Name END as name,
   Grades.Grade, Students.Marks 
FROM Students INNER JOIN Grades 
ON Students.Marks BETWEEN Grades.Min_mark AND Grades.Max_mark
ORDER BY Grades.Grade DESC, Students.Name;

答案 3 :(得分:0)

SELECT CASE WHEN grades.grade < 8 THEN NULL ELSE students.name END 
    as name,grades.grade,students.marks 
from students inner join grades on students.marks between min_mark and max_mark 
order by grades.grade desc,students.name;

答案 4 :(得分:0)

def upload_path_handler(self, filename):
    return "vault/static/vault/images/recordimages/r_{id}/{date}/{file}".format(id=self.id, file=filename, date=datetime.datetime.utcnow().strftime('%Y-%m-%d_%H-%M-%S'))

答案 5 :(得分:0)

SELECT 
CASE 
    WHEN G.Grade < 8 THEN NULL
    ELSE S.Name
END,
G.Grade, S.Marks
FROM Students AS S
JOIN Grades AS G
ON S.Marks BETWEEN G.Min_Mark AND G.Max_Mark
ORDER BY G.Grade DESC, S.Name ASC;

答案 6 :(得分:0)

那是我的回答,但他并不完美: 也许您可以解决

grades.grade, students.marks,
case
    when grades.grade < 8 then "null"
    else students.name
    end as NNN
from students
inner join grades on students.marks between grades.min_mark and grades.max_mark
order by grades.grade desc;

答案 7 :(得分:0)

您也可以尝试以下SQL查询:

  public bool CheckLimit()
    {
        try
        {
            var queryParams = new NameValueCollection()
            {
                { "p", "check_limit" },
            };

            string response = this.ApiCall(queryParams);
            dynamic json_response = JsonConvert.DeserializeObject(response);
            if (json_response.success == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception)
        {
            return false;
        }
    }

OR

SELECT CASE WHEN (STUDENTS.MARKS < 70) THEN 'NULL' ELSE STUDENTS.NAME END,
        GRADES.GRADE, STUDENTS.MARKS
FROM STUDENTS, GRADES
WHERE STUDENTS.MARKS >= GRADES.MIN_MARK AND STUDENTS.MARKS <= GRADES.MAX_MARK
ORDER BY GRADES.GRADE DESC, STUDENTS.NAME ASC;

答案 8 :(得分:0)

这在 MySQL 中对我有用 -

select if(Grade < 8, NULL , Name), Grade, Marks
from Students
Inner Join Grades
where Marks between Min_Mark AND Max_Mark
ORDER BY GRADE DESC, Name ASC;

答案 9 :(得分:-2)

SELECT 

  CASE 
  WHEN marks >= 70 THEN name
  ELSE NULL
  END AS req_name,
 
  CASE
  WHEN marks BETWEEN 0  AND 9  THEN 1
  WHEN marks BETWEEN 10 AND 19 THEN 2
  WHEN marks BETWEEN 20 AND 29 THEN 3
  WHEN marks BETWEEN 30 AND 39 THEN 4
  WHEN marks BETWEEN 40 AND 49 THEN 5
  WHEN marks BETWEEN 50 AND 59 THEN 6
  WHEN marks BETWEEN 60 AND 69 THEN 7
  WHEN marks BETWEEN 70 AND 79 THEN 8
  WHEN marks BETWEEN 80 and 89 THEN 9
  WHEN marks BETWEEN 90 and 100 THEN 10
  END AS grade,
 
  marks
 
FROM students
ORDER BY grade DESC, req_name, marks ASC