Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need a KQL query returning top X records each grouping

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading