简体   繁体   English

我想获取我将放置日期的数据,我将获取从过去三年到输入日期的数据

[英]I want to fetch the data where I will put the date and I will get data from last three years to the entered date

imagine if I put the date 27th Jan 2022 then I should get all the records from 26th Jan 2019 till 27th Jan 2022.想象一下,如果我输入 2022 年 1 月 27 日的日期,那么我应该得到从 2019 年 1 月 26 日到 2022 年 1 月 27 日的所有记录。

There are a couple of ways to do this.有几种方法可以做到这一点。 One caveat is how you are defining three years:一个警告是您如何定义三年:

select '2022-01-27'::date - interval '3 years';
      ?column?       
---------------------
 2019-01-27 00:00:00

or as you have it:或者你有它:

2019-01-26 to 2022-01-27 . 2019-01-26 to 2022-01-27

Using your definition:使用您的定义:

SELECT * FROM some_table WHERE date_fld BETWEEN '2019-01-26' and '2022-01-27';

--Using a date range

select * from  some_table where date_fld <@ daterange('2019-01-26', '2022-01-27', '[]');
 t
--or
select * from some_table where  date_fld <@ daterange('2019-01-26', '2022-01-28');
t

By default date ranges are exclusive on the upper bound, that is why you need to include either the '[]' or bump the upper bound up one date(which is actually what the '[]' does).默认情况下,日期范围在上限上是排他的,这就是为什么您需要包含'[]'或将上限提高一个日期(这实际上是'[]'所做的)。

For more information on ranges Ranges and Range operators有关范围的更多信息范围范围运算符

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PostgreSQL:如何从今天开始获取最近 2 年的数据 - PostgreSQL: How to get last 2 years of data from today's date 如何更改“ WHERE startdate&gt; = date”子句的值并获取日期范围的数据? - How can I change the value for “WHERE startdate >= date” clause and get data for a range of dates? PostgresSQL数据库具有日期字段,我希望除日期列以外的所有数据仅作为年份 - PostgresSQL database has date field, I want all data but the date column as year only 如何从最近x个月获取数据Postgres Sql Query哪里的日期字段是时间戳? - How to get data from last x months Postgres Sql Query where date field is a timestamp? 我想在 postgres SQL 中按日期和时间对我的数据进行分组,并希望每个 groupby 列中有一行 - I want to group by my data with date and time In postgres SQL and want one row of each groupby column 如何从 PostgreSQL 获取特定日期的特定数据? - How can i get specific data from a specific date from PostgreSQL? 从中获取数据以及到目前为止获取所有匹配结果 - fetch data from and to date to get all matching results 如果 from 和 to date 为空,如何获取过去 7 天的数据? - How to get last 7 days data if from and to date is null? 从具有创建日期的表中获取上周的数据 - Get last week's data from a table with a creation date 我想制作一个 function 从某些列获取数据 - I want to make a function that get data from some columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM