我看了几个数据库供应商,到目前为止只有Oracle似乎支持两种情况下的rank()函数;分析与分析聚合
以下是其文档的链接:https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions123.htm
我看过mySQL,Snowflake,e.t.c,但他们似乎只支持分析级别()。 有没有人在聚合上下文中使用任何其他RDBMS的排名,或者它通常是一种解决方法吗?
谢谢!
答案 0 :(得分:1)
据我所知,目前(2018年4月),只有两个数据库将此功能作为聚合实施:
Oracle - >演示 - > http://sqlfiddle.com/#!4/74fa70/2
SELECT rank( 2 ) within group (order by x) as rank_x FROM qwe;
SELECT rank( 2 ) within group (order by y) as rank_y FROM qwe;
SELECT rank( 2, 7 ) within group (order by x,y) as rank_x_y FROM qwe;
| RANK_X |
|--------|
| 5 |
| RANK_Y |
|--------|
| 2 |
| RANK_X_Y |
|----------|
| 7 |
PostgreSQL - >演示 - > http://sqlfiddle.com/#!17/74fa7/5
SELECT rank( 2 ) within group (order by x) as rank_x FROM qwe;
SELECT rank( 2 ) within group (order by y) as rank_y FROM qwe;
SELECT rank( 2, 7 ) within group (order by x,y) as rank_x_y FROM qwe;
| RANK_X |
|--------|
| 5 |
| RANK_Y |
|--------|
| 2 |
| RANK_X_Y |
|----------|
| 7 |
除了Oracle和PostgreSQL之外,分析版本rank() over
也已在SQL-Server,DB2,MySql中实现(将在即将发布的版本中)。
这两个演示基于以下示例数据:
CREATE TABLE qwe(
x int, y int
);
insert into qwe values(1,1);
insert into qwe values(1,2);
insert into qwe values(1,3);
insert into qwe values(1,4);
insert into qwe values(2,5);
insert into qwe values(2,6);
insert into qwe values(3,7);