[英]How can I populate a table in one month increments based on a date
I need to load a table based on the entry date from another table being between the 1st day of a month and the last day of the month. 我需要根据一个月的第一天到该月的最后一天之间的另一个表的输入日期来加载表。 I need to decrement the load to be 1 month's worth of records 1 month from the current month, then 2 months from the current month and so so.
我需要将负载从当前月份的1个月减少为1个月的记录,然后从当前月份开始2个月,依此类推。
I've tried CTE, using a cursor, and manually changing the datediff parameters. 我已经尝试过使用光标进行CTE,并手动更改datediff参数。 This last method works, but I would like to load the table dynamically
最后一种方法有效,但是我想动态加载表
Declare @StartDt as datetime = (SELECT FORMAT(DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())-1,0)),'MM/dd/yyyy'));
Declare @endDt as datetime = EOMONTH(@StartDt);
SELECT useruid,
userfullname,
sum(totaltime)SumTime,
@StartDt as StartDate,
@EndDt as EndDate
FROM Table_1.tbl
WHERE ENTRYDATE between @StartDt and @EndDt
group by useruid, USERFULLNAME;
I am inserting this into another table. 我将其插入另一个表。 I don't have an issue doing that by manually changing -1 in the Startdt variable to -2 and so on.
通过手动将Startdt变量中的-1更改为-2等,我这样做没有问题。 I want to know if I can have it change dynamically while loading the other table.
我想知道是否可以在加载另一个表时动态更改它。
Again, I have no issues loading the table. 同样,我没有问题加载表。 I just want to know if there is a more efficient way to do so as I have to go back 100 months.
我只想知道是否有更有效的方法,因为我必须回溯100个月。
One method is a recursive CTE: 一种方法是递归CTE:
with dates as (
select datefromparts(year(getdate()), month(getdate()), 1) as dte
union all
select dateadd(month, -1, dte)
from dates
where dte > ? -- how far back do you want to go?
)
select t.useruid, t.userfullname, sum(t.totaltime) as SumTime,
d.dte as StartDate,
dateadd(month, 1, d.dte) as EndDate
from dates d JOIN
Table_1.tbl t
on t.ENTRYDATE >= d.dte AND
t.ENTRYDATE < dateadd(month, 1, d.dte)
group by t.useruid, t.USERFULLNAME, d.dte;
You do not need a (an explicit) loop or a cursor. 您不需要(显式)循环或游标。
Note: You seem to be subtracting one second from the date for some reason. 注意:由于某种原因,您似乎要从日期中减去一秒钟。 I don't understand why.
我不明白为什么。 You may need this logic for end date, but it is better to use the inequalities that are in the
on
clause (so you don't have to worry about milliseconds, for instance). 您可能需要这种逻辑作为结束日期,但是最好使用
on
子句中的不等式(例如,您不必担心毫秒)。
You can use dynamic SQL to write your @StartDt declare statement on the fly. 您可以使用动态SQL即时编写@StartDt声明语句。
I put your code in the while loop which will run 100 times. 我将您的代码放入while循环中,该循环将运行100次。 (go back 100 months):
(返回100个月):
declare @int int = 0
while @int > -100
begin
set @int= @int -1
declare @str varchar(max)
set @str = 'declare @StartDt as datetime = (SELECT FORMAT(DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0,GETDATE())'+cast(@int as varchar(10))+',0)),''MM/dd/yyyy''))'
execute (@str)
Declare @endDt as datetime = EOMONTH(@StartDt)
SELECT useruid,
userfullname,
sum(totaltime)SumTime,
@StartDt as StartDate,
@EndDt as EndDate
FROM Table_1.tbl
WHERE ENTRYDATE between @StartDt and @EndDt
group by useruid, USERFULLNAME
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.