从表中选择特定月份的值包含timestamp列

时间:2015-05-07 12:10:17

标签: sql postgresql timestamp

例如,我有一个下面的表(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

那么如何才能在7个月内从tbl_trans找到tottal no.of交易?

所以预期的输出是

tot_tran  month
--------+-------
2         | July

4 个答案:

答案 0 :(得分:3)

select count(transaction_id) tot_tran
      ,to_char(max(transaction_dte),'Month') month from tbl_trans
where extract (month from transaction_dte)=7

PostgreSQL提取功能解释here

参考:Date/Time Functions and Operators

答案 1 :(得分:0)

select count(transaction_id),date_part('month',transaction_dte)
from tbli_trans where date_part('month',transaction_dte)=7

答案 2 :(得分:-1)

EXTRACT(MONTH FROM TIMESTAMP transaction_dte)

OR

date_part('month', timestamp transaction_dte)

如果时间戳以字符串格式保存,则只需添加单词timestamp

正确地查看了2之间的差异:

  

提取功能主要用于计算   处理。用于格式化显示的日期/时间值。

     

date_part函数以传统的Ingres等效函数为模型   到SQL标准函数extract。

答案 3 :(得分:-2)

使用Datepart功能。

where datepart(transaction_dte, mm) = 7
相关问题