简体   繁体   中英

How to query for grouped distinct records that both have null and non-null values in Kusto?

I am trying to create a query that returns a result set with a distinct (car) column based on another (data) column that is non-null.

In the example below, if there is a non-null value found in the data column then return the single instance with a value and if not, return the value with null and always maintain the distinctness of the first column.

let Car = datatable(car, data:string) 
[
    "mercedes", "fast",
    "mercedes", null,
    "tesla", null
    "toyota", "good",
    "sonata", null,
    "sonata", null,
    "sonata", "amazing" 
];

So the desired output would be:

"mercedes", "fast",
"tesla", null,
"toyota", "good",
"sonata", "amazing",

Thanks!

one option would be using a combination of set_difference() and make_set() :

  • make_set() will create a set of all unique values of data (by car , the aggregation key)
  • dynamic([""]) is an array with an empty string
  • set_difference() will produce the difference between the two former arrays - to provide a set with a non-empty string (or an empty set)
  • last, by accessing the first element of the result set (using [0] ), you'll get the first element that's not-empty (or null, if the set is empty)
datatable(car:string, data:string) 
[
    "mercedes", "",
    "mercedes", "fast",
    "tesla", "",
    "toyota", "good",
    "sonata", "",
    "sonata", "",
    "sonata", "amazing" 
]
| summarize data = set_difference(make_set(data), dynamic([""]))[0] by car
car data
mercedes fast
tesla
toyota good
sonata amazing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM