Assume a table called Cities like the following:
GA Augusta 203953
GA Athens 130873
GA Atlanta 522328
GA Savannah 151227
FL Orlando 328354
FL Tampa 399734
FL Miami 455075
FL Jacksonville 987960
I need a KQL query returning the top 2 cities ranked by population for each state.
For example, here is the expected result:
FL Jacksonville 987960
FL Miami 455075
GA Atlanta 522328
GA Augusta 203953
>Solution :
you can use the partition operator
for example:
datatable(state:string, city:string, population:long)
[
"GA", "Augusta", 203953,
"GA", "Athens", 130873,
"GA", "Atlanta", 522328,
"GA", "Savannah", 151227,
"FL", "Orlando", 328354,
"FL", "Tampa", 399734,
"FL", "Miami", 455075,
"FL", "Jacksonville", 987960,
]
| partition by state ( top 2 by population desc )
| state | city | population |
|---|---|---|
| GA | Atlanta | 522328 |
| GA | Augusta | 203953 |
| FL | Jacksonville | 987960 |
| FL | Miami | 455075 |