趋势线间隔30天-标签中重复月份

时间:2020-06-28 08:15:33

标签: sql-server sql-function trendline

使用以下查询检查趋势线的30天间隔。但是问题是它可以解决重复的月份

USE [Database];
DECLARE @toDate NVARCHAR(30)  = '2020-05-29';
DECLARE @T table(ID int, toDate date, fromDate date, xVal NVARCHAR(100), indexVal NVARCHAR(100))
    insert into @T values(1, Cast((select (Cast(@toDate as datetime))) as date), Cast((select (Cast(@toDate as datetime))-30) as date),'',0)
    insert into @T values(2, Cast((select (Cast(@toDate as datetime))-30) as date), Cast((select (Cast(@toDate as datetime))-60) as date),'',0)
    insert into @T values(3, Cast((select (Cast(@toDate as datetime))-60) as date), Cast((select (Cast(@toDate as datetime))-90) as date),'',0)
    insert into @T values(4, Cast((select (Cast(@toDate as datetime))-90) as date), Cast((select (Cast(@toDate as datetime))-120) as date),'',0)
    insert into @T values(5, Cast((select (Cast(@toDate as datetime))-120) as date), Cast((select (Cast(@toDate as datetime))-150) as date),'',0)
    insert into @T values(6, Cast((select (Cast(@toDate as datetime))-150) as date), Cast((select (Cast(@toDate as datetime))-180) as date),'',0)
    insert into @T values(7, Cast((select (Cast(@toDate as datetime))-180) as date), Cast((select (Cast(@toDate as datetime))-210) as date),'',0)
    insert into @T values(8, Cast((select (Cast(@toDate as datetime))-210) as date), Cast((select (Cast(@toDate as datetime))-240) as date),'',0)
    insert into @T values(9, Cast((select (Cast(@toDate as datetime))-240) as date), Cast((select (Cast(@toDate as datetime))-270) as date),'',0)
    insert into @T values(10, Cast((select (Cast(@toDate as datetime))-270) as date), Cast((select (Cast(@toDate as datetime))-300) as date),'',0)
    insert into @T values(11, Cast((select (Cast(@toDate as datetime))-300) as date), Cast((select (Cast(@toDate as datetime))-330) as date),'',0)
    insert into @T values(12, Cast((select (Cast(@toDate as datetime))-330) as date), Cast((select (Cast(@toDate as datetime))-360) as date),'',0)

    UPDATE @T SET 
        xVal = CONCAT(FORMAT(toDate , 'MMM'), '-', YEAR(toDate))
    select * from @T as T

enter image description here

2 个答案:

答案 0 :(得分:1)

签出以下一个,但也会存在缺陷,它指定错误的月份的日期

USE [database];
DECLARE @toDate NVARCHAR(30)  = '2020-03-01';
DECLARE @T table(ID int, toDate date, fromDate date, xVal NVARCHAR(100), indexVal NVARCHAR(100))
    insert into @T values(1, Cast((select (Cast(@toDate as datetime))) as date), Cast((select (Cast(@toDate as datetime))-30) as date),'',0)
    insert into @T values(2, Cast((select (Cast(@toDate as datetime))-30) as date), Cast((select (Cast(@toDate as datetime))-60) as date),'',0)
    insert into @T values(3, Cast((select (Cast(@toDate as datetime))-60) as date), Cast((select (Cast(@toDate as datetime))-90) as date),'',0)
    insert into @T values(4, Cast((select (Cast(@toDate as datetime))-90) as date), Cast((select (Cast(@toDate as datetime))-120) as date),'',0)
    insert into @T values(5, Cast((select (Cast(@toDate as datetime))-120) as date), Cast((select (Cast(@toDate as datetime))-150) as date),'',0)
    insert into @T values(6, Cast((select (Cast(@toDate as datetime))-150) as date), Cast((select (Cast(@toDate as datetime))-180) as date),'',0)
    insert into @T values(7, Cast((select (Cast(@toDate as datetime))-180) as date), Cast((select (Cast(@toDate as datetime))-210) as date),'',0)
    insert into @T values(8, Cast((select (Cast(@toDate as datetime))-210) as date), Cast((select (Cast(@toDate as datetime))-240) as date),'',0)
    insert into @T values(9, Cast((select (Cast(@toDate as datetime))-240) as date), Cast((select (Cast(@toDate as datetime))-270) as date),'',0)
    insert into @T values(10, Cast((select (Cast(@toDate as datetime))-270) as date), Cast((select (Cast(@toDate as datetime))-300) as date),'',0)
    insert into @T values(11, Cast((select (Cast(@toDate as datetime))-300) as date), Cast((select (Cast(@toDate as datetime))-330) as date),'',0)
    insert into @T values(12, Cast((select (Cast(@toDate as datetime))-330) as date), Cast((select (Cast(@toDate as datetime))-360) as date),'',0)
    UPDATE @T SET 
        xVal = CONCAT(FORMAT(DateAdd(month,-(ID-1),@todate) , 'MMM'), '-', YEAR(DateAdd(month,-(ID-1),@todate)))
    select * from @T as T
 

enter image description here

答案 1 :(得分:0)

您似乎正在尝试生成一系列开始和结束月份。月份的天数不尽相同,因此您想按月份而不是按天进行补偿。

我建议:

declare @toDate NVARCHAR(30)  = '2020-05-01'; -- beginning of the month
declare @T table(ID int, toDate date, fromDate date, xVal NVARCHAR(100), indexVal NVARCHAR(100));

insert into @T(ID, fromDate, toDate) values
    (1, @toDate, emonth(@toDate),
    (2, dateadd(month, -1, @toDate), emonth(dateadd(month, -1, @toDate)),
    (3, dateadd(month, -2, @toDate), emonth(dateadd(month, -2, @toDate)),
    (4, dateadd(month, -3, @toDate), emonth(dateadd(month, -3, @toDate)),
    ...
相关问题