我有一个表格score
,其中有列ID, ID_Member, Shot, Points, ScoreSeconds, ScoreDate
我需要从该表中获取前5名列表成员以及我的结果并显示排名
我希望选出得分最高的成员,分数秒数要少,并且要比其他成员更早完成
每个成员可以拍摄1至5张照片
我找到了一些解决方案,但其工作不正确, ID_Member = 4是我的id成员
select
t.Rank + 1 as Rank,
s.*
from (
select
s."ID_Member",
max(s."Points") as Score_MaxPoints,
max(s."ScoreSeconds") as Score_BestSeconds,
max(s."ScoreDate") as Score_BestDate
from "ScoreTest" as s
where s."Points" > 0
and s."ID_Member" <> 4
group by s."ID_Member"
union select
s."ID_Member",
max(s."Points") as Score_MaxPoints,
max(s."ScoreSeconds") as Score_BestSeconds,
max(s."ScoreDate") as Score_BestDate
from "Score" as s
where s."Points" > 0
and s."ID_Member" = 4
group by s."ID_Member"
order by
2 desc,
3 asc,
4 asc
limit 6
) as s
left join lateral (
select
count(distinct o."ID_Member") as Rank
from "ScoreTest" as o
where o."ID_Member" <> s."ID_Member"
and ( (o."Points" > s.Score_MaxPoints)
or ( o."Points" = s.Score_MaxPoints
and o."ScoreSeconds" > s.Score_BestSeconds)
or ( o."Points" = s.Score_MaxPoints
and o."ScoreSeconds" = s.Score_BestSeconds
and o."ScoreDate" > s.Score_BestDate))
) as t on true
order by 1;
我的桌子score
:
create table if not exists "Score" (
"ID" serial primary key,
"ID_Member" integer,
"Shot" integer,
"Points" integer,
"ScoreSeconds" integer,
"ScoreDate" timestamp with time zone
)
要插入的数据:
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (1, 1, 12, 77, '2018-10-28 02:43:52.67967+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (1, 2, 27, 336, '2018-10-28 02:51:03.585303+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (2, 1, 9 , 66, '2018-10-28 18:47:28.352804+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (2, 2, 9 , 114, '2018-10-31 19:26:22.926129+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (2, 3, 10, 158, '2018-10-31 22:13:21.906929+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (3, 1, 22, 158, '2018-11-01 19:44:48.097315+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (3, 2, 22, 154, '2018-11-05 19:37:13.108119+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (4, 1, 10, 69, '2018-11-05 21:58:44.744138+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (4, 2, 18, 142, '2018-11-06 05:54:32.502891+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (5, 1, 37, 459, '2018-11-06 05:58:07.265676+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (5, 2, 23, 151, '2018-11-06 19:51:44.677442+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (6, 1, 17, 126, '2018-11-06 20:41:39.969008+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (7, 1, 90, 1114, '2018-11-06 20:45:09.458599+03');
insert into "ScoreTest" ("ID_Member", "Shot", "Points", "ScoreSeconds", "ScoreDate") values (8, 1, 23, 154, '2018-11-06 20:45:09.458599+03');
UPD:添加了预期的输出
rank | member | points | seconds | date
1 | 7 | 90 | 1114 | 2018-11-06 20:45:09.458599+03
2 | 5 | 37 | 459 | 2018-11-06 05:58:07.265676+03
3 | 1 | 27 | 336 | 2018-10-28 02:51:03.585303+03
4 | 8 | 23 | 154 | 2018-11-06 20:45:09.458599+03
5 | 3 | 22 | 154 | 2018-11-05 19:37:13.108119+03
6 | 4 | 18 | 142 | 2018-11-06 05:54:32.502891+03