我从stackoverflow获得的以下示例查询,这很有帮助。我使用相同的方案,它已部分解决。
工作查询
SELECT *
FROM Requirements
ORDER BY
CASE Day
WHEN 'Monday' THEN 1
WHEN 'Tuesday' THEN 2
WHEN 'Wednesday' THEN 3
WHEN 'Thursday' THEN 4
WHEN 'Friday' THEN 5
WHEN 'Saturday' THEN 6
WHEN 'Sunday' THEN 7
END
我的查询输出如下,我的表有重复值,Monday1在Monday1之后返回,但为什么在星期二之前返回Tuesday1?
Monday
Monday1
Tuesday1
Tuesday
Wednesday
Wednesday1
我的表有两个列值,如下所示:
Monday Monday
Monday Monday1
Wednesday Wednesday
Wednesday Wednesday1
Tuesday Tuesday
Tuesday Tuesday1
感谢。
答案 0 :(得分:2)
将包含Monday1
等的列添加到ORDER BY语句:
;WITH Requirements AS (
SELECT *
FROM (VALUES
('Monday','Monday'),
('Monday','Monday1'),
('Wednesday','Wednesday'),
('Wednesday','Wednesday1'),
('Tuesday','Tuesday'),
('Tuesday','Tuesday1')
) as t([day], col2)
)
SELECT *
FROM Requirements
ORDER BY
CASE Day
WHEN 'Monday' THEN 1
WHEN 'Tuesday' THEN 2
WHEN 'Wednesday' THEN 3
WHEN 'Thursday' THEN 4
WHEN 'Friday' THEN 5
WHEN 'Saturday' THEN 6
WHEN 'Sunday' THEN 7
END , col2
输出:
day col2
Monday Monday
Monday Monday1
Tuesday Tuesday
Tuesday Tuesday1
Wednesday Wednesday
Wednesday Wednesday1
答案 1 :(得分:0)
您只对第一列进行排序,第二列不涉及任何ORDER BY,因此没有关于如何排序column2的规则。 Column1按预期排序。如果您希望在星期一之后的Monday1,请将column2添加为第二个ORDER BY参数