sql 聚合函數和group by 聯合使用


原文

很多時候單獨使用聚合函數的時候覺得很容易,求個平均值,求和,求個數等,但是和分組一起用就有點混淆了,好記性不如爛筆頭,所以就記下來以后看看。

 

常用聚合函數羅列

  1. 1

    AVG() - 返回平均值

    COUNT() - 返回行數

    FIRST() - 返回第一個記錄的值

    LAST() - 返回最后一個記錄的值

    MAX() - 返回最大值

    MIN() - 返回最小值

    SUM() - 返回總和

    END

創建一張表

  1. 1

    CREATE TABLE [dbo].[stuscore](

        [name] [varchar](50)  ,--學生名字

        [subject] [varchar](50) ,--課程名字

        [score] [int] ,--分數

        [stuid] [int] ,--學號    [d_date] datetime,--登記時間

    )

     

  2. 2

    添加多條數據

    insert into [stuscore]select '張三','數學','78','1',GETDATE()union allselect '張三','語文','98','1',GETDATE()union allselect '張三','英語','88','1',GETDATE()union allselect '李四','數學','81','2',GETDATE()union allselect '李四','語文','83','2',GETDATE()union allselect '李四','英語','60','2',GETDATE()

    sql 聚合函數和group by 聯合使用
  3. 3

    問題羅列:

    1.    計算每個人的總成績並排名(要求顯示字段:姓名,總成績)

    2.    計算每個人的總成績並排名(要求顯示字段: 學號,姓名,總成績,時間)

    3.    計算每個人單科的最高成績(要求顯示字段: 學號,姓名,課程,最高成績)

    4.    計算每個人的平均成績(要求顯示字段: 學號,姓名,平均成績)

    5.    列出各門課程成績最好的學生(要求顯示字段: 學號,姓名,科目,成績)

    6.    列出各門課程成績最好的兩位學生(要求顯示字段: 學號,姓名,科目,成績)

    7.    統計如下:

     

    學號  姓名 語文 數學 英語 總分 平均分

     

     

     

    8.列出各門課程的平均成績(要求顯示字段:課程,平均成績)

    9.列出數學成績的排名(要求顯示字段:學號,姓名,成績,排名)

    10.列出數學成績在2-3名的學生(要求顯示字段:學號,姓名,科目,成績)

    11.求出李四的數學成績的排名

    12.統計如下:

     

    課程 不及格(0-59)個    良(60-80)個    優(81-100)個

     

     

     

     

     

     

     

     

     

     

     

     

     

    END

問題解決 

  1. 1

     計算每個人的總成績並排名(要求顯示字段:姓名,總成績)

    select name, SUM(score) as totalscore from stuscore group by name  order by totalscore desc

    (求和用sum,計算每個人按name 分組,排序order by)

    sql 聚合函數和group by 聯合使用
  2. 2

     計算每個人的總成績並排名(要求顯示字段: 學號,姓名,總成績)

    select stuid,SUM(score) as totalscore,name,d_date from stuscore group by name,stuid ,d_date order by totalscore desc

    (如果一個表有很多字段呢?難道也要一個一個字段羅列出來group by?)

    select distinct t1.name,t1.stuid,t2.allscore,t1.d_date from  stuscore t1,

    (

        select stuid,sum(score) as allscore from stuscore group by stuid

    )t2

    where t1.stuid=t2.stuid

    order by t2.allscore desc

     

    sql 聚合函數和group by 聯合使用
  3.  

    計算每個人單科的最高成績(要求顯示字段: 學號,姓名,課程,最高成績) select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,

    (

    select stuid,max(score) as maxscore from stuscore group by stuid

    ) t2

    where t1.stuid=t2.stuid and t1.score=t2.maxscore

     

    sql 聚合函數和group by 聯合使用
  4.  

    計算每個人的平均成績(要求顯示字段: 學號,姓名,平均成績)

    select distinct t1.stuid,t1.name,t2.avgscore from stuscore t1,(select stuid,avg(score) as avgscore from stuscore group by stuid) t2where t1.stuid=t2.stuid

    sql 聚合函數和group by 聯合使用
  5.  

     列出各門課程成績最好的學生(要求顯示字段: 學號,姓名,科目,成績)

    select distinct t1.stuid,t1.name,t1.score from stuscore t1,

    (select subject ,max(score) as maxscore from stuscore group by subject) t2

    where t1.score=t2.maxscore and t1.subject=t2.subject

     

    sql 聚合函數和group by 聯合使用
  6.  

     統計如下:

     

    學號  姓名 語文 數學 英語 總分 平均分

     

     

     select stuid as 學號,name as 姓名, sum(case when subject='語文' then score else 0 end) as 語文, sum(case when subject='數學' then score else 0 end) as 數學, sum(case when subject='英語' then score else 0 end) as 英語, sum(score) as 總分,(sum(score)/count(*)) as 平均分 from stuscore group by stuid,name  order by 總分 desc

     

  7.  

    列出各門課程成績最好的兩位學生(要求顯示字段: 學號,姓名,科目,成績)

    select * from stuscore t1 where t1.stuid in (select top 2 stuscore.stuid  from stuscore where subject=t1.subject   order by score desc)order by t1.subject

    sql 聚合函數和group by 聯合使用
     
     
  8.  

    列出各門課程的平均成績(要求顯示字段:課程,平均成績)

    select subject,AVG(score) from stuscore group by subject

    sql 聚合函數和group by 聯合使用
     
     
  9.  

    列出數學成績的排名(要求顯示字段:學號,姓名,成績,排名)

    declare @temp table(pm int identity(1,1),stuid int,name varchar(50),score int)insert into @temp select stuid,name,score from stuscore where subject='數學' order by score descselect * from @temp

    sql 聚合函數和group by 聯合使用
     
     
  10.  

    列出數學成績在2-3名的學生(要求顯示字段:學號,姓名,科目,成績)

    select t3.*  from

    (

    select top 2 t2.*  from (

    select top 3 name,subject,score,stuid from stuscore where subject='數學'

    order by score desc

    ) t2 order by t2.score

    ) t3 order by t3.score desc

     

  11.  

    求出李四的數學成績的排名

    declare @tmp table(pm int,name varchar(50),score int,stuid int)insert into @tmp select null,name,score,stuid from stuscore where subject='數學' order by score descdeclare @id intset @id=0;update @tmp set @id=@id+1,pm=@idselect * from @tmp where name='李四'

  12.  

    統計如下:

     

    課程 不及格(0-59)個    良(60-80)個    優(81-100)個

     

     

     

    select subject, (select count(*) from stuscore where score<60 and subject=t1.subject) as 不及格,(select count(*) from stuscore where score between 60 and 80 and subject=t1.subject) as 良,(select count(*) from stuscore where score >80 and subject=t1.subject) as 優from stuscore t1 group by subject

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM