1.
select 歌曲ID,sum(1) as 學習數次,sum(測試成績) as 累積分數,max(測試時間(最近瀏覽日期)) as 最近瀏覽日期
from(select * from SongStudentScore where 學生ID = 需要查询的学生ID) as one
group by 歌曲ID2.其他都好算排名要用过程算
(select 学生ID as 排名,学生ID,累積分數,學習次數,累積分數/學習次數 as 平均分,最近瀏覽日期
from(select 学生ID,sum(測試成績) as 累積分數,sum(1) as 學習次數,max(測試時間(最近瀏覽日期)) as 最近瀏覽日期 from SongStudentScore
group by 学生ID) as one order by 累積分數/學習次數) 这个语句会用到过程中--写过程
create or replace function paimin() returns setof record as $$
declare
ax record;
bx varchar;
begin
bx='1';
for ax in
select 学生ID as 排名,学生ID,累積分數,學習次數,累積分數/學習次數 as 平均分,最近瀏覽日期
from(select 学生ID,sum(測試成績) as 累積分數,sum(1) as 學習次數,max(測試時間(最近瀏覽日期)) as 最近瀏覽日期 from SongStudentScore
group by 学生ID) as one order by 累積分數/學習次數
loop
ax.排名=bx;
return next ax;
bx=bx+1;
end loop;
return;
end;
$$ language plpgsql
调用的时候类型一定要一致
select * from paimin() a(排名 varchar,学生ID varchar,累積分數 numeric,學習次數 numeric,平均分 numeric,最近瀏覽日期 date)