如果你有 SQL SERVER 2005 或更高版本,你可以这样做
SELECT
*,
CALC.sortlist
from events as e
outer apply
(
select
case
WHEN ((e.month > 9 OR (e.month =9 and e.days >= 22)) AND e.year <= 2012)then 'list1'
WHEN (e.year <= 2013) then 'list2'
WHEN (e.year > 2013)then 'list3'
end as sortlist
) as CALC
order by
CALC.sortlist, CALC.date,
case when CALC.sortlist = 'list3' then e.year else null end
你也可以这样做,但如果你的情况等于'list3',它并不完全按年份排序(我的意思是如果你要改变表达式,case
你也需要改变order by
)
SELECT
*,
case
WHEN ((e.month > 9 OR (e.month =9 and e.days >= 22)) AND e.year <= 2012)then 'list1'
WHEN (e.year <= 2013) then 'list2'
WHEN (e.year > 2013)then 'list3'
end as sortlist
from events as e
order by
sortlist, e.date,
case when e.year > 2013 then e.year else null end