库斯托 | KQL:将动态列扩展到两个的所有组合(Couples | Tuples)

Kusto | KQL: Expand dynamic column to all combinations of two ( Couples | Tuples )

我有一个场景,我试图创建一个视图,向我显示每个键的所有唯一值对。例如:

datatable(Key:string, Value:string)[
'1', 'A', 
'2', 'B',
'2', 'C', 
'3', 'A', 
'3', 'B', 
'3', 'C', 
'3', 'C']
| sort by Key, Value asc
| summarize Tuples=make_set(Value) by Key  

结果:

Key Tuples
1   ["A"]
2   ["B","C"]
3   ["A","B","C"]

想要的结果:

Key Tuples
1   ["A"]
2   ["B","C"]
3   ["A","B"]
3   ["A","C"]
3   ["B","C"]

如何在 KQL 中实现这一点?

这是一种不太优雅也不高效的方法,它使用内部自连接来获取每个键的所有组合

datatable(Key:string, Value:string)
[
    '1', 'A', 
    '2', 'B',
    '2', 'C', 
    '3', 'A', 
    '3', 'B', 
    '3', 'C', 
    '3', 'C'
]
| distinct Key, Value
| as hint.materialized=true T1
| join kind=inner T1 on Key
| where Value != Value1
| project Key, Tuple = tostring(array_sort_asc(pack_array(Value, Value1)))
| distinct Key, Tuple
| as hint.materialized=true T2
| union (
    T1
    | where Key !in ((T2 | project Key)) | project Key, Tuple = tostring(pack_array(Value))
)
| order by Key asc, Tuple asc
Key Tuple
1 ["A"]
2 ["B","C"]
3 ["A","B"]
3 ["A","C"]
3 ["B","C"]