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

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

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

问题

我有一个查询

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

我得到这些值

010423
020423
070423
140423
260523
280423
290923
291223
300623
310323

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

英文:

I have a query

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

Im getting this values

010423
020423
070423
140423
260523
280423
290923
291223
300623
310323

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

答案1

得分: 1

只返回翻译好的部分:

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

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

SELECT last("date_of_maturity") dom
  FROM "maturity_date_data"
 GROUP BY "maturity";
 ORDER BY to_date(dom, 'ddMMyy') asc

答案2

得分: 0

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

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:

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:

确定