【发布时间】: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