简体   繁体   中英

Oracle Rank() analytic function

If I put 3 names in a row:

Kovács Albert, Kovács Dávid, Nagy Balázs

Surename is Kovács, Nagy

FamilyName is Albert, Dávid, Balázs

And if I run the rank() analytic function to Familyname, the output what will be?

1-Kovács Albert
1-Kovács Dávid
3-Nagy Balázs

OR

1-Kovács Dávid
1-Kovács Albert
3-Nagy Balázs

OR will it be random?

I have no oracle installed in to my computer, if you can give me some online free learning instance, I would appreciate it...

If you do a query like this on Familyname . Output will be like this:

1-Kovács Albert
1-Kovács Dávid
1-Nagy Balázs

With this query:

SELECT
  RANK() OVER(PARTITION BY FamilyName ORDER BY FamilyName) AS iRank,
  Surename,
  FamilyName
FROM
  Table1

If you do the RANK on SureName and order by FamilyName . The output will be like this:

1-Kovács Albert
2-Kovács Dávid
1-Nagy Balázs

With this query:

SELECT
  RANK() OVER(PARTITION BY Surename ORDER BY FamilyName) AS iRank,
  Surename,
  FamilyName
FROM
  Table1

It depends entirely on what you want. To get either of your proposed results your analytic clause would have to be rank() over ( order by surname ) . This query provides you with the rank of every record in the order of their surname ascending. As you have two people with the same surname then the order of those two people should theoretically be random. This is because rank() does not actually do any ordering of the result-set, you need an order by clause to do this; or at least to guarantee the results you want.

Something like:

select rank() over ( order by surname asc, familyname asc ) as rnk
     , surname, familyname
  from my_table
 order by rank() over ( order by surname asc, familyname asc ) 

This query returns the surname, family name and rank in order of ascending surname and family name. The result-set is ordered by the rank, meaning that it would look like this:

1 | Kovács | Albert
2 | Kovács | Dávid
3 | Nagy   | Balázs

This is the benefit and the problem with analytic functions, you can be massively specific in what you wish to do but if you want a specific answer you have to be exact in the query that you run.

The documentation provides another example using the partition by clause. If we were to apply this to your query, partitioning by surname and ordering by familyname your query and result set could look like this.

select rank() over ( partition by surname order by familyname asc ) as rnk
     , surname, familyname
  from my_table

2 | Kovács | Dávid
1 | Kovács | Albert
1 | Nagy   | Balázs

This means that we rank all the familynames for each surname individually. As Dávid is after Albert he get's a rank of 2 in the surname Kovács. Note that with no specific order by there's no requirement for the result-set to be ordered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM