我的目的是查出近期节日,比如现在9月20号,则9月20号及以后的排在前面,到12月份时就从1月份排起到9月19号,我的语句如下,怎样把查询结果 合并。或者有其他更好的语句?
select ID,FestDay,DAY(FestDay),GETDATE() from ER_ForeignCulture where Class1=2 and Class2=3 and (MONTH(FestDay)>MONTH(GETDATE()) or (MONTH(FestDay)=MONTH(GETDATE()) and Day(FestDay)>=Day(GETDATE()))) order by MONTH(FestDay),DAY(FestDay);
select ID,FestDay,DAY(FestDay),GETDATE() from ER_ForeignCulture where Class1=2 and Class2=3 and ID not in(select ID from ER_ForeignCulture where Class1=2 and Class2=3 and (MONTH(FestDay)>MONTH(GETDATE()) or (MONTH(FestDay)=MONTH(GETDATE()) and Day(FestDay)>=Day(GETDATE())))) order by MONTH(FestDay),DAY(FestDay);
union all
谢了,不过还少了点东西,自己解决了。
支持一楼
最终改成这样(select * from (select TOP 100 PERCENT * from ER_ForeignCulture where Class1=2 and Class2=3 and (MONTH(FestDay)>MONTH(GETDATE()) or (MONTH(FestDay)=MONTH(GETDATE()) and Day(FestDay)>=Day(GETDATE()))) order by MONTH(FestDay),DAY(FestDay))a)
union all
(select * from (select TOP 100 PERCENT * from ER_ForeignCulture where Class1=2 and Class2=3 and ID not in(select ID from ER_ForeignCulture where Class1=2 and Class2=3 and (MONTH(FestDay)>MONTH(GETDATE()) or (MONTH(FestDay)=MONTH(GETDATE()) and Day(FestDay)>=Day(GETDATE())))) order by MONTH(FestDay),DAY(FestDay))b)
主要是union all和order by的共存问题,加上Top 100 PERCENT解决