PostgreSQL,最小和最大日期之间的行数 [英] PostgreSQL, count of rows between min and max dates

查看:125
本文介绍了PostgreSQL,最小和最大日期之间的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过查询从文本列获取最小和最大日期,好人帮助我获取了那里是这样的:

I am getting min and max dates from text column with query which good people helped me to get there like this :

SELECT max(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')),
min(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY'))
FROM table_name;

我如何获取COUNT之间的所有匹配行,包括最小和最大日期(写在文本列中) )?

How can I get COUNT of all matched rows between and including min and max dates (written in text column)?

推荐答案

您真正需要做的就是:

SELECT count(*), max(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')), min(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')) 
  FROM table_name;

或者我可能误会了。实际上,如果可能的话,应该将日期字段移动到日期类型。如果必须处理垃圾输入,请使用视图和更新触发器来完成此操作。

Or maybe I misunderstood. Really, you should, if at all possible, move the date field to a date type. If you have to handle garbage input, use a view and an update trigger to do that.

这篇关于PostgreSQL,最小和最大日期之间的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆