【发布时间】:2021-03-05 11:01:56
【问题描述】:
我有两个结构完全相同的表
table 1 - Data_2020 --> This is an static table which has data from year 2020 and is not being updated anymore (archive table). It has around 4 million records
table 2 - Data_2021 --> This is my current table which is increasing everyday. It has currently 0.8 million records but it will increase till December.
现在我需要“合并”这两个表,并且每次运行以下查询时我只想要最近 13 个月的数据
Select * from Data_2020
union all
select * from Data_2021
我必须每个月运行一次,并且只需要最近 13 个月的数据。如何应用过滤器?我在两个表中都有一个日期列“日期”。
【问题讨论】:
-
将不同年份的同一种数据保存在不同的表中可能不是最好的设计选择(对于像这样的查询)。你有没有考虑过类似
select * from ( <current union query goes here> ) where date >= dateadd(month, -13, getdate());的东西? -
为什么不同的表,都放在一张表里。 20年后你会有20张桌子??
标签: sql sql-server select