【问题标题】:Trendline 30 days interval - month repeated in labels趋势线 30 天间隔 - 标签中重复的月份
【发布时间】:2020-06-28 08:15:33
【问题描述】:

使用以下查询检查趋势线的 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

【问题讨论】:

  • 请以表格文本形式向我们展示您想要的结果。
  • 我需要按月拆分标签,它应该以 30 天为间隔。在结果中没有如下重复 5 月 4 月 3 月 2 月 1 月 12 月 11 月 10 月 9 月 8 月 7 月 @GMB

标签: sql-server sql-function trendline


【解决方案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
 

【讨论】:

    【解决方案2】:

    您似乎正在尝试生成一系列月份的开始和结束时间。月份有不同的天数,因此您希望按月而不是按天来抵消。

    我会推荐:

    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)),
        ...
    

    【讨论】:

    • 但它再次出现在月份间隔而不是 30 天间隔之下,它将使所需的功能/特性无效。为了您的信息,它将以任何日期开始@toDate
    • @Rishi:只需将@toDate 替换为datefromparts(year(@toDate), month(@toDate), 1)
    • 但它不会满足@todate 作为当前日期和从现在起持续 30 天它会一直通过 todate 作为每个月的月初
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2017-03-25
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多