sql-server - 趋势线 30 天间隔 - 标签中重复的月份
问题描述
使用以下查询检查趋势线的 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
解决方案
查看以下内容,但它也有缺陷,指定错误月份的错误日期
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
推荐阅读
- c# - 将 C# API 请求转换为 PHP
- r - 使用 geom_text 在 ggplot2 中的单个方面注释文本
- jquery - 让 Jquery 和 Bootstrap 与 Rails 6 一起工作
- python - 从python中的列表中有效地删除也在给定列表中的元素
- matlab - 在 MATLAB GUI 中的选择之间重置弹出菜单
- python - 以递归方式评估 json 的优雅方法
- vbscript - 是否有一个函数或命令可用于在我的代码中调用默认浏览器
- azure-devops - Azure DevOps:用户没有完成此操作的权限。你需要有'AddPackage'
- c# - 如何让本地数据库在客户的 PC 上工作
- elm - 用于生成变量和函数的 Elm 语法:如何区分?