Sql 按组对行进行排序的查询
我正在使用ApacheDerby10.10 我有一份参与者名单,并希望计算他们在本国的排名,如下所示:Sql 按组对行进行排序的查询,sql,derby,rank,row-number,groupwise-maximum,Sql,Derby,Rank,Row Number,Groupwise Maximum,我正在使用ApacheDerby10.10 我有一份参与者名单,并希望计算他们在本国的排名,如下所示: | Country | Participant | Points | country_rank | |----------------|---------------------|--------|--------------| | Australia | Bridget Ciriac | 1 | 1 | |
| Country | Participant | Points | country_rank |
|----------------|---------------------|--------|--------------|
| Australia | Bridget Ciriac | 1 | 1 |
| Australia | Austin Bjorklun | 4 | 2 |
| Australia | Carrol Motto | 7 | 3 |
| Australia | Valeria Seligma | 8 | 4 |
| Australia | Desmond Miyamot | 27 | 5 |
| Australia | Maryjane Digma | 33 | 6 |
| Australia | Kena Elmendor | 38 | 7 |
| Australia | Emmie Hicke | 39 | 8 |
| Australia | Kaitlyn Mund | 50 | 9 |
| Australia | Alisia Vitaglian | 65 | 10 |
| Australia | Anika Bulo | 65 | 11 |
| UK | Angle Ifil | 2 | 1 |
| UK | Demetrius Buelo | 12 | 2 |
| UK | Ermelinda Mell | 12 | 3 |
| UK | Adeline Pee | 21 | 4 |
| UK | Alvera Cangelos | 23 | 5 |
| UK | Keshia Mccalliste | 23 | 6 |
| UK | Alayna Rashi | 24 | 7 |
| UK | Malinda Mcfarlan | 25 | 8 |
| United States | Gricelda Quirog | 3 | 1 |
| United States | Carmina Britto | 5 | 2 |
| United States | Noemi Blase | 6 | 3 |
| United States | Britta Swayn | 8 | 4 |
| United States | An Heidelber | 12 | 5 |
| United States | Maris Padill | 21 | 6 |
| United States | Rachele Italian | 21 | 7 |
| United States | Jacquiline Speake | 28 | 8 |
| United States | Hipolito Elami | 45 | 9 |
| United States | Earl Sayle | 65 | 10 |
| United States | Georgeann Ves | 66 | 11 |
| United States | Conchit Salli | 77 | 12 |
架构如下所示:
这就是我尝试过的:
select
Country.name,
Participant.name,
Participant.points,
ROW_NUMBER() OVER(order by Country.name, Participant.points) as country_rank
from Country
join Team
on Country.id = Team.country_id
join Participant
on Team.id = Participant.team_id;
但是根据,OVER语句不接受任何参数
有人有办法获得国家排名吗?您只需要添加一个按国家划分的分区,这将为您提供所需的内容
SELECT
Country.name,
Participant.name,
Participant.points,
ROW_NUMBER() OVER(PARTITION BY country order by Country.name, Participant.points) as country_rank
from Country
join Team
on Country.id = Team.country_id
join Participant
on Team.id = Participant.team_id;
考虑使用相关聚合计数子查询的非windows函数SQL查询。由于组列Country.name与排名标准Participant.points不在同一个表中,因此我们需要在子查询中运行相同的联接,但重命名表别名以正确比较内部查询和外部查询 当然,在一个完美的世界里,情况就是这样,但我们现在必须考虑平局。因此,将使用另一个非常类似的tie breaker子查询添加到第一个子查询中。第二个嵌套查询匹配内部和外部查询的Country.name和Participant.points,但按Participant.name的字母顺序排列 SQL 在线演示 SQL Fiddle演示: 解释
可以使用一个简单的ANSI-SQL子选择来完成相同的工作,计算同一国家/地区中分数较低或分数相同且名称字母顺序不高的参与者的记录数。谢谢JVM,在Apache Derby中按分区可以工作吗?我试过了,但它说我在第x行第I列遇到了分区,我对ApacheDerby不太熟悉,但我在google上搜索了一下,发现它们确实存在于ApacheDerby中。不幸的是,这是一个链接,那个wiki页面描述了构建此类功能的设计方案,并不是所有的功能最终都得到了构建。向您致敬!你更擅长打平手!
SELECT
Country.name,
Participant.name,
Participant.points,
ROW_NUMBER() OVER(PARTITION BY country order by Country.name, Participant.points) as country_rank
from Country
join Team
on Country.id = Team.country_id
join Participant
on Team.id = Participant.team_id;
SELECT
Country.name AS Country,
Participant.name AS Participant,
Participant.points,
(SELECT Count(*) + 1
FROM Country subC
INNER JOIN Team subT
ON subC.id = subT.country_id
INNER JOIN Participant subP
ON subT.id = subP.team_id
WHERE subC.name = Country.name
AND subP.points < Participant.points)
+
(SELECT Count(*)
FROM Country subC
INNER JOIN Team subT
ON subC.id = subT.country_id
INNER JOIN Participant subP
ON subT.id = subP.team_id
WHERE subC.name = Country.name
AND subP.points = Participant.points
AND subP.name < Participant.name) As country_rank
FROM Country
INNER JOIN Team
ON Country.id = Team.country_id
INNER JOIN Participant
ON Team.id = Participant.team_id
ORDER BY Country.name, Participant.points;
SELECT c.name AS Country,
p.name AS Participant,
p.points AS Points,
(SELECT COUNT(*)
FROM Participant p2
JOIN Team t2 ON p2.team_id = t2.id
WHERE t2.country_id = t.country_id
AND (p2.points < p.points
OR p2.points = p.points AND p2.name <= p.name)) AS country_rank
FROM Country c
JOIN Team t ON c.id = t.country_id
JOIN Participant p ON t.id = p.team_id
ORDER BY c.name, p.points, p.name;