Clube 2 or more KQL query into single query.

Ameet Desai 0 Reputation points

Hi Team,


I would like to club two or more KQL queries into one single query so that I can get consolidated result, please help me on the same.

Query-1: List out Vnets, subnets, location and resource group.


| where type == ''

| extend subnets = properties.subnets

| mv-expand subnets

| project name,,, location, resourceGroup


Query-2: List out NSGs and associated resource groups.


| where type =~ ""

| project name, resourceGroup, subscriptionId

| sort by name asc



Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,229 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Sina Salam 5,551 Reputation points

    Hi Ameet Desai

    Thank you for reaching out.

    The problem is to combine the two queries into one.

    You can use union to combine them for the same result as expected.

    union Query1, Query2
    let Query1 = Resources
    | where type == ''
    | extend subnets = properties.subnets
    | mv-expand subnets
    | project name,,, location, resourceGroup;
    let Query2 = Resources
    | where type =~ ""
    | project name, resourceGroup, subscriptionId
    | sort by name asc;
    union Query1, Query2

    Kindly let me know if this work for you.


    0 comments No comments

  2. KapilAnanth-MSFT 38,201 Reputation points Microsoft Employee

    @Ameet Desai ,

    Welcome to the Microsoft Q&A Platform. Thank you for reaching out & I hope you are doing well.

    I am not sure why you would like to query NSGs and Subnets together, as this would mean "" and "" would be empty (null) for the NSGs

    However, the below query should do the trick

    | where type == '' or type == ''
    | extend subnets = properties.subnets
    | mv-expand subnets
    | project name,,, location, resourceGroup, ['type'], subscriptionId
    | sort by ['type'], name asc
    | project-away ['type']

    P.S :

    • I am sorting based on Resource Type so that NSGs appear together and the Subnets appear together.
    • The NSGs and Subnets themselves are sorted in Ascending order withing their group

    Please let us know if we can be of any further assistance here.



    Please Accept an answer if correct.

    Original posters help the community find answers faster by identifying the correct answer.

    0 comments No comments