表中有姓名,科目成績欄位,怎樣求出平均成績,并輸出學生的成績表呢?
uj5u.com熱心網友回復:
if OBJECT_ID('tempdb..#test')is not null drop table test
create table #test(姓名 nvarchar(10),科目 nvarchar(10),成績 decimal(4,0))
insert into #test (姓名,科目,成績)
select '張三','數學',56 union all
select '張三','英語',57 union all
select '張三','語文',59 union all
select '李四','數學',60 union all
select '李四','英語',61 union all
select '李四','語文',62 union all
select '李四','自習',63
select 姓名,CAST(AVG(成績)as decimal(4,2)) [平均分] from #test group by 姓名
/*
李四 61.50
張三 57.33*/
select * from #test
pivot
(
Max(成績)
for 科目 in (數學,英語,語文,自習) ---第二步
) as #test
/*
李四 60 61 62 63
張三 56 57 59 NULL*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/26173.html
標籤:疑難問題
