简体   繁体   English

如何根据日期以一个月为增量填充表格

[英]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.

相关问题 如何在一个数据库表中获得两个自动增量? - How can I get two auto-increments in one database table? 如何从表格中获取基于月份和年份的记录? - How can I get records based on month and year from table? 脚本以基于一个表中的日期字段填充每月汇总表 - script to populate monthly summary tables based on the date field in one table 如何根据另一个表中的值填充一个表中的空值,其中连接基于子字符串 - How do I populate null values in one table based on values in another, where the join is based on a substring 我可以创建一个 SQL (Oracle) 触发器,根据另一个表的更新增加每日计数吗? - Can I create a SQL (Oracle) Trigger that increments a daily count based off another table's updates? 如何在一个月内根据日期对查询结果进行分组 - How to group query results based on date in one month 如何基于日期参数用月列创建临时表? - How to create Temp Table with month columns based on date parameters? 如何在一个表中使用另一个表中的FK填充我的FK列? - How can I populate my FK column in one table with the FK from another table? 如何根据提供的日期在表中找到时间戳值 - how can i find a timestamp value in a table based on a date provided 如何根据同一表中的另一列数据填充一列? - How To Populate One Column Based On Another Columns Data In Same Table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM