Skip to content

Top N by Value ​

Use @aggregate to order by a computed value instead of alphabetically. This shows the top 5 occupations ranked by total income.

Interactive Example ​

Try It
TABLE
  ROWS occupation[-5@income.sum]
  COLS education * gender * income.sum
;
Dataset:samplesoccupation, education, gender, income
Try Variations

Query Breakdown ​

  • TABLE - Declares a crosstab table statement
  • occupation[-5@income.sum] - Top 5 occupations ordered by sum of income (descending)
    • [-5] - Negative number means descending order (highest first), limit to 5
    • @income.sum - Order by this aggregate value, not alphabetically
  • COLS education * gender - Two-level column hierarchy: education containing gender
  • income.sum - Measure: sum of income for each cell

Without @, limits are alphabetical. With @aggregate, they're ranked by that computed value.

Powered by Malloy and DuckDB