SELECT TOP 1
hol_id ,
CONVERT(VARCHAR(12), hol_date, 112)
FROM holiday
WHERE hol_id = 5
UNION ALL
SELECT hol_id ,
CONVERT(VARCHAR(12), hol_date, 112)
FROM holiday
WHERE hol_id <> 5
ORDER BY CONVERT(VARCHAR(12), hol_date, 112) DESC
我看到了
1 20131218
5 20131018
6 20130818
3 20130405
4 20130311
2 20121129
我想看看
5 20131018
1 20131218
6 20130818
3 20130405
4 20130311
2 20121129
答案 0 :(得分:0)
您应该按实际日期(hol_date
)排序,而不是按转化的varchar值排序。
假设hol_id
为表格的 Key column
(不确定为什么在这里使用TOP 1),请尝试使用ORDER BY CASE
进行此查询,如下所示:
SELECT hol_id ,
CONVERT(VARCHAR(12), hol_date, 112)
FROM holiday
ORDER BY CASE WHEN hol_id = 5
THEN DATEADD(year,100,getdate()) --Assuming 100 years as max
ELSE hol_date
END DESC
答案 1 :(得分:0)
我会计算一个新值来将特定记录移到顶部,如:
order by
case when hol_id = 5 then 0 else 1 end -- Sort hol_id 5 at the top
, hol_date desc -- Sort the rest according to date