如何从Influx数据库查询中按升序获取日期,目前日期以字符串格式呈现。

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

how to get dates sorted in ascending order from influx db query, right now its in string format

问题

我有一个查询

  1. SELECT last("date_of_maturity") FROM "maturity_date_data" GROUP BY "maturity";

我得到这些值

  1. 010423
  2. 020423
  3. 070423
  4. 140423
  5. 260523
  6. 280423
  7. 290923
  8. 291223
  9. 300623
  10. 310323

该字段是字符串格式,但值是日期。
需要按日期顺序输出。

英文:

I have a query

  1. SELECT last("date_of_maturity") FROM "maturity_date_data" GROUP BY "maturity";

Im getting this values

  1. 010423
  2. 020423
  3. 070423
  4. 140423
  5. 260523
  6. 280423
  7. 290923
  8. 291223
  9. 300623
  10. 310323

The field is string format, but the values are dates.
Need output arranged datewise.

答案1

得分: 1

只返回翻译好的部分:

  1. 将您的字符串日期转换为实际日期,并按日期排序:
  2. ```sql
  3. SELECT last("date_of_maturity") dom
  4. FROM "maturity_date_data"
  5. GROUP BY "maturity";
  6. ORDER BY to_date(dom, 'ddMMyy') asc
英文:

You could to convert your string date to actual date, and order by it:

  1. SELECT last("date_of_maturity") dom
  2. FROM "maturity_date_data"
  3. GROUP BY "maturity";
  4. ORDER BY to_date(dom, 'ddMMyy') asc

答案2

得分: 0

如果这是一个Python问题,它与influxdbgrafanaregex(你的标签)无关。问题变成了:如何按照日期格式DDMMYY对日期列表进行按时间顺序排序?答案是一个Python一行代码,如果你假设日期以6位数字字符串的形式存储在名为results的列表中:

  1. results.sort(key=lambda x: x[4:6]+x[2:4]+x[0:2])
英文:

If this is a python question, it has nothing to do with influxdb or grafana or regex (your tags). The question becomes: How do I sort a list of dates in the format DDMMYY in chronological order? The answer is a python one liner, if you assume the dates are in a list of 6-digit strings in a list called: results:

  1. results.sort(key=lambda x: x[4:6]+x[2:4]+x[0:2])

huangapple
  • 本文由 发表于 2023年3月31日 22:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899452.html
匿名

发表评论

匿名网友

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

确定