我正在尝试根据范围创建一个设置日期,以便为可视化创建一个设置。这是非常重复的,不是未来的证明。有没有人对如何简化这个有任何想法?
Select CASE WHEN date BETWEEN '2010-08-01 00:00:00' AND '2011-07-31 12:59:59'
THEN '2011-05-31 00:00:00'
WHEN date BETWEEN '2011-08-01 00:00:00' AND '2012-07-31 12:59:59'
THEN '2012-05-31 00:00:00'
WHEN date BETWEEN '2012-08-01 00:00:00' AND '2013-07-31 12:59:59'
THEN '2013-05-31 00:00:00'
WHEN date BETWEEN '2013-08-01 00:00:00' AND '2014-07-31 12:59:59'
THEN '2014-05-31 00:00:00'
WHEN date BETWEEN '2014-08-01 00:00:00' AND '2015-07-31 12:59:59'
THEN '2015-05-31 00:00:00'
WHEN date BETWEEN '2015-08-01 00:00:00' AND '2016-07-31 12:59:59'
THEN '2016-05-31 00:00:00'
WHEN date BETWEEN '2016-08-01 00:00:00' AND '2017-07-31 12:59:59'
THEN '2017-05-31 00:00:00'
WHEN date BETWEEN '2017-08-01 00:00:00' AND '2018-07-31 12:59:59'
THEN '2018-05-31 00:00:00'
WHEN date BETWEEN '2018-08-01 00:00:00' AND '2019-07-31 12:59:59'
THEN '2019-05-31 00:00:00'
WHEN date BETWEEN '2019-08-01 00:00:00' AND '2020-07-31 12:59:59'
THEN '2020-05-31 00:00:00'
END AS modifiedDate,
FROM表1
答案 0 :(得分:0)
您基于某种奇怪的日期算法(大概是某种会计年度),希望5月31日成为种子。
所有这些都可以使用日期算术来计算。没有数据库标签,很难指定正确的语法。但是,这个想法是:
select (date '2011-05-31' +
(extract(year from date + interval '5 month') - 2011) * interval '1 year'
)
这是用ISO / ANSI SQL表示的,但是这个想法可以在任何数据库中表达-尽管任何给定的数据库也可能具有其他方法。
在SQL Server中,这将是:
select dateadd(year,
(extract(year from date + interval '5 month') - 2011),
'2011-05-31'
)
答案 1 :(得分:0)
在你们回答之前从另一个站点得到了它,效果很好:
Case when month(date)>=8 then datefromparts(year(date)+1,5,31)
when month(date)<8 then datefromparts(year(date),5,31)
end as modifiedDate
也感谢您的回答。帮助我解决了其他几个问题。