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

Kusto equivalent of SQL NOT IN

I am trying to identify what records exist in table 1 that are not in table 2 (so essentially using NOT IN)

let outliers =
Table 2
| project UniqueEventGuid;
Table 1
|where UniqueEventGuid !in  (outliers)
|project UniqueEventGuid

but getting 0 records back even though I know there are orphans in table 1.
Is the !in not the right syntax?

Thanks in advance!

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 :

!in operator

"In tabular expressions, the first column of the result set is
selected."

In the following example I intentionally ordered the column such that the query will result in error due to mismatched data types.

In your case, the data types might match, so the query is valid, but the results are wrong.

let t1 = datatable(i:int, x:string)[1,"A", 2,"B", 3,"C" ,4,"D" ,5,"E"];
let t2 = datatable(y:string, i:int)["d",4 ,"e",5 ,"f",6 ,"g",7];
t1
| where i !in (t2)

Relop semantic error: SEM0025: One of the values provided to the
‘!in’ operator does not match the left side expression type ‘int’,
consider using explicit cast

Fiddle

If that is indeed the case, you can reorder the columns or project only the relevant one.
Note the use of double brackets.

let t1 = datatable(i:int, x:string)[1,"A", 2,"B", 3,"C" ,4,"D" ,5,"E"];
let t2 = datatable(y:string, i:int)["d",4 ,"e",5 ,"f",6 ,"g",7];
t1
| where i !in ((t2 | project i))

Another option is to use leftanti join

i x
1 A
2 B
3 C

Fiddle

let t1 = datatable(i:int, x:string)[1,"A", 2,"B", 3,"C" ,4,"D" ,5,"E"];
let t2 = datatable(y:string, i:int)["d",4 ,"e",5 ,"f",6 ,"g",7];
t1
| join kind=leftanti t2 on i
i x
2 B
3 C
1 A

Fiddle

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