表中的任何事件会在当前日/月之前提前一天发生* this * year。任何在当天/月之后到期日/月的事件将发生下一个年。明年到期的任何活动比今年任何活动都要多。
现在说,你要求的是不可能的,因为你要求...a View, sorted by...
这是一个不存在的概念。 意见没有排序。只有查询已排序。所以,你可以创建一个项目适当的活动日期,然后从视图中查询必须使用ORDER BY度日事件日期排序的事件视图:
create table Events (
id int identity(1,1) not null,
DueDay int not null,
DueMonth int not null);
go
insert into Events (DueDay, DueMonth)
values (1,1), (15,11), (31,12);
go
create view eventsDate as
select id, dateadd(day, DueDay-1,
dateadd (month, DueMonth-1,
dateadd(year,
case when DueMonth < month(getdate()) or
(DueMonth = month(getdate()) and
DueDay < day(getdate()))
then year(getdate())-1899
else year(getdate())-1900 end,
'19000101'))) as DueDate
from Events;
要获取订单的事件,查询必须包括一个订单:
select * from eventsDate order by DueDate desc;
您是否只计划持有最多一年的数据? – 2010-11-16 00:10:23
是的,表可能是错的。谢谢大家的所有优秀答案! – Aximili 2010-11-16 04:06:05