首页 > 解决方案 > 复制过去月份的数据

问题描述

我有一张表格,每天都会在其中捕获数据。例如:列是帐户、资金编号、金额、日期。假设数据被捕获到 2020 年 12 月 31 日,下一个数据从 2021 年 3 月 1 日开始。现在,我的要求是我想从上次输入的数据(即 12 月 31 日)中填充 2021 年 1 月 1 日至 2021 年 2 月 28 日的数据, 2020.我想复制2020年12月31日的数据,填写2021年1月1日到2月28日。

可能吗?请让我知道解决方案。

标签: sql-server

解决方案


你可以通过运行一个while循环来做到这一点

DECLARE @currentDate Date;
set @currentDate = '2021-01-01'
WHILE @currentDate <= '2021-02-28' 
BEGIN
    insert into yourtable(account, fundnumber, amount, date) select account, fundnumber, amount, @currentDate from yourtable where date='2020-12-31'
    SET @currentDate = DATEADD(day, 1, @currentDate)
END

推荐阅读