获得每所学校男女生平均得分最高的GPA

时间:2019-04-22 05:50:52

标签: sql sql-server

我有桌子

TBL_STD_MARKS 
SCHOOL_ID   SYMBOL_NO       AVG_GPA
1               211             3.8
1               255             3.5
1               212             2.90
2               311             3.85
2               312             3.75
3               411             2.98
3               412             3.00


TBL_STD_INFO
SCHOOL_ID   SYMBOL_NO   STUDENT_NAME    GENDER
1               211         S1              M   
1               255         S2              F
1               212         S3              M
2               311         S22             M
2               312         S23             M   
3               411         S31             M
3               412         S32             F   


TBL_SCHOOL
SCHOOL_ID       SCHOOL_NAME
1               SCHOOL11
2               SCHOOL22
3               SCHOOL33

我正在尝试编写一个查询,以获取每所学校的男女学生得分最高的AVG_GPA。但是我的查询是给出表的所有行。

 WITH cte
    AS (
        SELECT D.SCHOOL_NAME
            ,A.SYMBOL_NO
            ,B.STUDENT_NAME         
            ,B.GENDER
            ,A.AVG_GPA
        FROM TBL_STD_MARKS A
        INNER JOIN TBL_STD_INFO B ON A.SYMBOL_NO = B.SYMBOL_NO
        INNER JOIN TBL_SCHOOL D ON A.SCHOOL_ID = D.SCHOOL_ID
        )
    SELECT SCHOOL_NAME
            ,SYMBOL_NO
            ,STUDENT_NAME       
        ,MAX(DISTINCT CASE 
                WHEN GENDER = 'M'
                    THEN AVG_GPA
                END) AS MALE_GPA
        ,MAX(DISTINCT CASE 
                WHEN GENDER = 'F'
                    THEN AVG_GPA
                END) AS FEMALE_GPA
    FROM CTE
    GROUP BY SCHOOL_NAME,SYMBOL_NO
            ,STUDENT_NAME

6 个答案:

答案 0 :(得分:2)

使用row_number()窗口功能

     with cte as (  
          SELECT D.SCHOOL_NAME
        ,A.SYMBOL_NO
        ,B.STUDENT_NAME         
        ,B.GENDER
        ,A.AVG_GPA,row_number()over(partition by D.SCHOOL_NAME, B.GENDER order by A.AVG_GPA desc) rn
    FROM TBL_STD_MARKS A
    INNER JOIN TBL_STD_INFO B ON A.SYMBOL_NO = B.SYMBOL_NO
    INNER JOIN TBL_SCHOOL D ON A.SCHOOL_ID = D.SCHOOL_ID
    ) select * from cte where rn=1

答案 1 :(得分:0)

public onClick(event: MouseEvent): void {
    if (event.which === 2 ){
        /** Middle mouse click */
        this.router.navigate(['/b', this.blogPost.Id, this.blogPost.url])
    } else {
        /** Normal click */
        this.showComments = true;
    }
}

答案 2 :(得分:0)

尝试一下

SELECT tbs.SCHOOL_ID,
CASE WHEN GENDER = 'F' THEN
      MAX(AVG_GPA) END AS HighestFemaleGPA,
CASE WHEN GENDER = 'M' THEN
      MAX(AVG_GPA) END AS HighestMaleGPA
FROM @TBL_STD_MARKS stdM
INNER JOIN @TBL_SCHOOL tbs
ON  tbs.SCHOOL_ID = stdM.SCHOOL_ID
INNER JOIN @TBL_STD_INFO stdI
ON stdI.SYMBOL_NO = stdM.SYMBOL_NO
GROUP BY tbs.SCHOOL_ID,GENDER

答案 3 :(得分:0)

SELECT std_marks.SCHOOL_ID,school.SCHOOL_NAME, std_info.GENDER, MAX(std_marks.AVG_GPA) 
as MAX_AVG_GPA
FROM TBL_STD_MARKS std_marks
INNER JOIN TBL_STD_INFO std_info ON std_info.SYMBOL_NO = std_marks.SYMBOL_NO
INNER JOIN TBL_SCHOOL school ON school.SCHOOL_ID = std_marks.SCHOOL_ID
GROUP BY std_info.GENDER, std_marks.SCHOOL_ID, school.SCHOOL_NAME
ORDER BY std_marks.SCHOOL_ID

答案 4 :(得分:0)

选择ts.School_Id,ts.School_Name,tsi.Gender,max(tsm.Avg_GPA)作为来自Tbl_School ts的Avg_GPA  ts.School_Id = tsi.School_Id上的Tbl_Std_Info tsi加入Tbl_Std_Marks tsm  在ts.School_Id,ts.School_Name,tsi.Gender上按tsi.Symbol_No = tsm.Symbol_No分组

enter image description here

答案 5 :(得分:0)

假设这是我们的表。以下查询将为您提供帮助:

Select a.SchoolId, a.Gender,
  (Select max(GPA) From [Test].[guest].[School] where SchoolId = a.SchoolId and Gender = 
  a.Gender) as GPA
  from [Test].[guest].[School] a
  Group by SchoolId, Gender