表中的任何事件會在當前日/月之前提前一天發生* 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