Search code examples
azure-data-explorerkql

How to convert row count result to another column in Kusto query


I've a table like below and want to add another column which contains the row count of the result.

datatable(Name: string, salary: int) 
["a", 500,
"a", 500,
"b", 600, 
"b", 600, 
"c", 800, 
"d", 1900,
"e", 200]

Kusto query:

datatable
| summarize by Name, Salary

My output should be summarized by each Name and also need the total row count of summarize query in separate column. In the above example, after summarize(), I end up getting 5 names by salary. However, having a challenge to get rowcount() in a separate column.

Name, Salary, RowCount
"a"      1000      5
"b"      1200      5 
"c"      800      5 
"d"     1900      5
"e"      200      5

Solution

  • you could try this:

    let T = datatable(Name: string, salary: int) 
    [
        "a", 500,
        "a", 500,
        "b", 600, 
        "b", 600, 
        "c", 800, 
        "d", 1900,
        "e", 200
    ]
    ;
    let RowCount = toscalar(T | summarize dcount(Name))
    ;
    T
    | summarize sum(salary) by Name
    | extend RowCount
    
    Name sum_salary RowCount
    a 1000 5
    b 1200 5
    c 800 5
    d 1900 5
    e 200 5