Search code examples
azure-data-explorerkqlazure-monitor

How to combine values (count) from different queries into a single query


Is there any way in KQL we can combine values (count) from different queries into a single query.

Currently the way I did was have two queries, get the count. Paste the values in third query and find the percentage (please refer below).

// First query
let football_played = database("database1").games_played
| where game_type contains "football"
| where Time > ago(1m);
football_played| count

// Second query
let registered_for_football = database("db2").registerd_players
| where EventInfo_Time > ago(1m)
| where registered_game contains "football"
registered_for_football | count

// Third query
let football_count = 13741;
let registered_count = 701588;
print cnt1 = football_count , cnt2 = registered_count 
| extend percentage = (todouble(cnt1) * 100 / todouble(cnt2))
| project percentage, cnt2, cnt1

Is there any way in Kql I can calculate everything in a single query and print the percentage?

Thanks in advance


Solution

  • you could try this:

    let football_played = toscalar(
        database("database1").games_played
        | where Time > ago(1m)
        | where game_type contains "football"
        | count
    );
    let registered_for_football = toscalar(
        database("db2").registered_players
        | where EventInfo_Time > ago(1m)
        | where registered_game contains "football"
        | count
    );
    print football_played, registered_for_football 
    | extend percentage = round(100.0 * football_played / registered_for_football, 2)