例如,我有一个如下表(Tbl_trans)
transaction_id transaction_dte
integer timestamp without time zone
---------------+----------------------------------
45 | 2014-07-17 00:00:00
56 | 2014-07-17 00:00:00
78 | 2014-04-17 00:00:00那么,我如何才能从tbl_trans中找到7个月内的全部tbl_trans事务呢?
所以预期的输出是
tot_tran month
--------+-------
2 | July发布于 2015-05-07 12:13:19
select count(transaction_id) tot_tran
,to_char(max(transaction_dte),'Month') month from tbl_trans
where extract (month from transaction_dte)=7PostgreSQL提取函数解释这里
参考资料:日期/时间函数和运算符
发布于 2015-05-07 12:14:00
select count(transaction_id),date_part('month',transaction_dte)
from tbli_trans where date_part('month',transaction_dte)=7发布于 2015-05-07 12:18:43
EXTRACT(MONTH FROM TIMESTAMP transaction_dte)或
date_part('month', timestamp transaction_dte)如果您的时间戳是以字符串格式保存的,则只需要添加时间戳。
正确地查找现在2之间的区别:
提取函数主要用于计算处理。用于格式化显示的日期/时间值。 date_part函数是在传统的Ingres基础上建模的,相当于SQL标准函数的提取.
https://stackoverflow.com/questions/30100703
复制相似问题