sql - PostgreSQL 中两个日期之间的差异

标签 sql postgresql date-arithmetic

PostgreSQL 中是否有与此 T-SQL 命令等效的命令?

SELECT COUNT(*) 
FROM [dbo].[LayerTable] 
where layerType=3 
 and created >= Dateadd(Month, Datediff(Month, 0, DATEADD(m, -6, current_timestamp))),0)

我找到了 date_part() 和 extract function() 但不起作用。

最佳答案

看起来这只是从 current_timestamp 中减去 6 个月,所以等价的是:

SELECT COUNT(*) 
FROM dbo.layer_table
where layer_type=3 
  and created >= current_timestamp - interval '6 months';

如果您想要月初(而不是与“今天”“同一天”)作为结果,请使用:

created >= date_trunc('month', current_timestamp - interval '6 months')

关于sql - PostgreSQL 中两个日期之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53257345/

相关文章:

r - 在有序的日期列表中填写缺少的年份

php - 查询失败(使用 mssql_query() 和 php)

php - MySql 连接查询左连接

php 更新查询列表只有一些工作没有错误

jsp - 在 JSTL EL 中获取当前日期并对其进行算术运算

sql - 将 2 个月添加到当前时间戳

python - 通过按列值选择 SqlAlchemy 创建字典组

sql - 将 array_agg 输出转换为数组而不是字符串

sql - 按总和过度分配

database - PostgreSQL 管理和监控