Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Tuesday, April 20, 2010

rank()

rank() over (partition by X... order by Y....) produces, for each row, its rank when all the rows are subdivided according to (basically, grouped by) fields X... and sorted within each subdivision by fields Y...

So if with this query

select story_name, episode, rating from episode

Space Museum,1,4
Space Museum,2,3
Space Museum,3,2
Space Museum,4,2
Castrovalva,1,3
Castrovalva,2,2
Castrovalva,3,4
Castrovalva,4,4

you wanted the episodes of each story to be ranked in order of rating, you'd specify

select story_name, episode, rating, rank() over (partition by story_name, episode order by rating desc) from episode

and get

Space Museum,1,4,1
Space Museum,2,3,2
Space Museum,3,2,3
Space Museum,4,2,3
Castrovalva,3,4,1
Castrovalva,4,4,1
Castrovalva,2,2,3
Castrovalva,1,3,4

NB that you get a sportsman's rank, not a mathematician's (two equal firsts get 1, not 2).

NB2 that you don't seem to have to specify all the candidate fields for partition, as you would with group by.

Followers

Blog Archive