简体   繁体   English

从日期 sql teradata 过滤去年

[英]filter last year from date sql teradata

please I have a table that contains data from 2020-01-01 till 2021-02-01 and I need to select the data for the running last 12 months to be from 2021-02-01 Back to 2020-02-01 as an example.请我有一个表格,其中包含从 2020-01-01 到 2021-02-01 的数据,我需要 select 将过去 12 个月的运行数据从 2021-02-01 返回到 2020-02-01 作为例子。 I need an optimized query as the table has a very huge amount of data 130Millions row我需要一个优化的查询,因为表有非常大量的数据 130Millions 行

thanks a lot.多谢。

Calculate the date range based on CURRENT_DATE, the optimizer resolves this to hard-coded dates:根据 CURRENT_DATE 计算日期范围,优化器将此解析为硬编码日期:

where datecol between add_months(trunc(current_date, 'mon'),-12)
                  and trunc(current_date, 'mon')

Assuming the table is partitioned by datecol this will result in partition elimination , ie scanning only the required partitions.假设表是按 datecol 分区的,这将导致分区消除,即只扫描所需的分区。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM