ADX Kusto Self Join with a combination of equality and inequality

databricksuser-5173 0 Reputation points
2025-04-14T23:00:47.3466667+00:00

Hi,

How do we generate Kusto query to achieve a self-join query, where one column matches but the other doesn't?

For example:

myTable

| where myColumnSubset == "mySubGroup"

| join kind=inner (

    myTable

    | where myColumnSubset == "mySubGroup"

    | project myColumnOne, myColumnTwo, myColumnThree

)

on $left.myColumnOne == $right.myColumnOne

The above query works.

If I add below further inequality criterion, it fails

$left.myColumnTwo != $right.myColumnTwo

Thank you

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
561 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ganesh Gurram 6,460 Reputation points Microsoft External Staff
    2025-04-15T18:36:00.82+00:00

    @databricksuser-5173

    I tried reproducing the scenario from my end, please find the below screenshots:

    User's image

    This is by design, as the Kusto engine optimizes joins using hash-based equality logic. To work around this, the recommended approach is to perform the equality join first and then apply any inequality filters in a subsequent where clause.User's image

    To further validate this behavior, I ran a working repro using the built-in StormEvents sample data. Here’s a complete working example that performs a self-join on an equality column (EventType), and then applies an inequality condition (State != State):

    User's image

    StormEvents
    | where State != "FLORIDA" // Filter out FLORIDA
    | extend leftState = State // Rename for clarity
    | join kind=inner (
        StormEvents
        | where State != "FLORIDA"
        | extend rightState = State
        | project rightState, EventType, DamageProperty
    ) on $left.EventType == $right.EventType // Equality join
    | where leftState != rightState // Apply inequality condition
    | summarize event_count = count() by leftState
    | where event_count > 1
    | project State = leftState, event_count
    
    

    Alternate approach:

    Could you please try the below approach:

    myTable
    | where myColumnSubset == "mySubGroup"
    | join kind=inner (
        myTable
        | where myColumnSubset == "mySubGroup"
        | project myColumnOne, myColumnTwo, myColumnThree
    ) on $left.myColumnOne == $right.myColumnOne
    | where myColumnTwo != myColumnTwo
    

    I hope this information helps!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.