题目链接:点击打开链接
题目大意:略。
解题思路:原题此解决方案是需要一个前提——美洲学生最多,否则第 7 个用例通不过,如图所示,但题目没说,有点坑;进阶题是即使没有说明哪个洲学生最多也可以解(列转行思想)。
AC 代码
--原题WITHt1AS(SELECT*, ROW_NUMBER() OVER(ORDERBYname) rnkFROMstudentWHEREcontinent='America'), t2AS(SELECT*, ROW_NUMBER() OVER(ORDERBYname) rnkFROMstudentWHEREcontinent='Asia'), t3AS(SELECT*, ROW_NUMBER() OVER(ORDERBYname) rnkFROMstudentWHEREcontinent='Europe') SELECTt1.nameAmerica, t2.nameAsia, t3.nameEuropeFROMt1LEFTJOINt2ONt1.rnk=t2.rnkLEFTJOINt3ONt1.rnk=t3.rnk--进阶selectmax(casewhencontinent='America'thennameelsenullend) asAmerica ,max(casewhencontinent='Asia'thennameelsenullend) asAsia ,max(casewhencontinent='Europe'thennameelsenullend) asEuropefrom (SELECT*, row_number() over(partitionbycontinentorderbyname) asrnfromstudent) tgroupbyrn