计算姓氏和订购最常见的

时间:2014-12-07 16:46:30

标签: mysql sql

我们希望找到德国学生(NATIONALITY)最常见的姓氏(LNAME)。创建一个视图,对于每个德国姓氏,显示姓氏和具有该姓氏的学生人数。输出将按最常见到最不常见的顺序排序,次要排序顺序按字母顺序升序。

到目前为止,我有这个

CREATE VIEW GermanSurnames AS
SELECT LNAME, COUNT(LNAME)
FROM STUDENT
WHERE NATIONALITY = 'GERMAN'
ORDER BY LNAME DESC

这是正确的还是我应该怎么做? 谢谢:))

2 个答案:

答案 0 :(得分:2)

你错过了这个小组

SELECT LNAME, COUNT(*) as NUMSTUDENTS
FROM STUDENT
WHERE NATIONALITY = 'GERMAN'
GROUP BY LNAME
ORDER BY COUNT(*) DESC, LNAME

答案 1 :(得分:1)

为什么要观看?不是

SELECT LNAME, COUNT(*) AS HOWMANY
  FROM STUDENT
 WHERE NATIONALITY = 'GERMAN'
 GROUP BY LNAME
 ORDER BY HOWMANY DESC;

足够?