Cross-row computation and proportion

From the STATES table in the database, query the data of State name, abbreviation, and population. After sorting by population, compute the population difference between each state and the other state ranking ahead of it, and compute the population proportion of each State.

Solution:

A
1
=demo.query("select NAME,ABBR,POPULATION from STATES")
2
=A1.sort@z(POPULATION)
3
=A2.sum(POPULATION)
4
=A2.derive(Difference,Proportion)
5
>A4.(Difference=if(#==1,0,POPULATION[-1]-POPULATION))
6
>A4.(Proportion=round(POPULATION/A3,4))

The computation in A4 is shown below: