Kusto基于另一个表的值构建新表。

huangapple go评论133阅读模式
英文:

Kusto to construct a new table based on values from another table

问题

需要在Kusto中根据另一个表中的值构建新表的帮助:
我有一个这样的表格:

  1. 月末日期
  2. 2023-04-30 0
  3. 2023-05-31 1
  4. 2023-06-30 0

我需要构建类似这样的日期:

  1. 打印日期部分=范围(todatetime('2023-05-28'), todatetime('2023-07-03'), 1d)
  2. | mvexpand日期部分

并使用我的先前表格中的这些新日期范围来填写值。意思是,如果日期>=2023-06-30或最新日期,则用值列中的值0填写,如果在2023-06-30和2023-05-31之间,则用2023-05-31中的相应值1填写,如果小于2023-05-31,则用2023-04-30中的相应值0填写。date_part的构建可以不同,可以只使用extend并根据指定的范围添加日期,以便更好地实现结果。

英文:

Need help in Kusto to construct a new table based on values from another table:
I have a table like this:

  1. month_end_date value
  2. 2023-04-30 0
  3. 2023-05-31 1
  4. 2023-06-30 0

I need to construct dates like this:

  1. print date_part=range(todatetime('2023-05-28'), todatetime('2023-07-03), 1d)
  2. | mvexpand date_part

and fill out values from my previous table with these new date ranges. meaning if the date is >=2023-06-30 or the latest date, fill out with the value in value column which is 0, and if it is between 2023-06-30 and 2023-05-31, with respected value in 2023-05-31 which is 1, and if less than 2023-05-31, with respected value for 2023-04-30 which is 0.
construction of date_part could be different like just using extend and add dates based on the range specified, whatever helps achieve better result.

答案1

得分: 1

我在我的环境中复制并获得了如下期望的结果:

以下是对我有效的KQL查询:

  1. let originalTable = datatable(month_end_date: datetime, value: dynamic)
  2. [
  3. datetime(2023-04-30), 0,
  4. datetime(2023-05-31), 1,
  5. datetime(2023-06-30), 0
  6. ];
  7. let dateRange=range x from todatetime('2023-05-28') to todatetime('2023-07-03') step 1d;
  8. let r=originalTable
  9. | join kind=fullouter (
  10. dateRange
  11. ) on $left.month_end_date == $right.x
  12. | extend date1=iff(isnull(month_end_date),x,month_end_date)
  13. | order by date1 asc
  14. | project-away month_end_date,x
  15. | sort by date1 asc
  16. | extend rn=row_number();
  17. r
  18. | project date1,value
  19. | summarize c = make_list_with_nulls(value)
  20. | extend d = series_fill_forward(c)
  21. | mv-expand d = d
  22. | project-away c
  23. | extend valuess= toint(d)
  24. | extend new_column = 0
  25. | sort by new_column asc
  26. | extend rn=row_number()
  27. | join kind=inner r on rn
  28. | project-away rn, rn1,value,new_column,d

输出:

第一部分
Kusto基于另一个表的值构建新表。

第二部分
Kusto基于另一个表的值构建新表。

Fiddle

尝试按照上述查询来获取我所得到的期望结果。

英文:

I have reproduced in my environment and got expected results as below:

Below is the KQL query which worked for me:

  1. let originalTable = datatable(month_end_date: datetime, value: dynamic)
  2. [
  3. datetime(2023-04-30), 0,
  4. datetime(2023-05-31), 1,
  5. datetime(2023-06-30), 0
  6. ];
  7. let dateRange=range x from todatetime('2023-05-28') to todatetime('2023-07-03') step 1d;
  8. let r=originalTable
  9. | join kind=fullouter (
  10. dateRange
  11. ) on $left.month_end_date == $right.x
  12. | extend date1=iff(isnull(month_end_date),x,month_end_date)
  13. |order by date1 asc
  14. |project-away month_end_date,x
  15. | sort by date1 asc
  16. | extend rn=row_number();
  17. r
  18. | project date1,value
  19. | summarize c = make_list_with_nulls(value)
  20. | extend d = series_fill_forward(c)
  21. | mv-expand d = d
  22. |project-away c
  23. |extend valuess= toint(d)
  24. | extend new_column = 0
  25. | sort by new_column asc
  26. | extend rn=row_number()
  27. | join kind=inner r on rn
  28. |project-away rn, rn1,value,new_column,d

Kusto基于另一个表的值构建新表。

Output:

1st part
Kusto基于另一个表的值构建新表。

2nd part

Kusto基于另一个表的值构建新表。

Fiddle.

Try to follow to follow above query to get desired result as I have got.

huangapple
  • 本文由 发表于 2023年7月11日 05:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定