是否可以在 SQL Server 2008 中选择特定的 ORDER BY? [英] Is it possible to select a specific ORDER BY in SQL Server 2008?

查看:32
本文介绍了是否可以在 SQL Server 2008 中选择特定的 ORDER BY?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含日期和时间的表,即 day 列,可以输入 7 天中的任何一天,并且它们被设置为数据类型 varchar.由于此表包含客户的预订时间,我想从表中选择 id 匹配的所有日期,并且我想按星期一至星期日的日期排序.我希望我可以在这个查询中添加一些东西来手动选择结果返回的顺序:

I have a table that holds days and times, the day column, can have any of the seven days entered into it, and they are set to data type varchar. As this table holds booking times for a client, I want to select all days from the table where the id matches, and I want to sort by day Monday-Sunday. I was hoping that I could add something to this query to manually select the order the results come back like so:

select * 
from requirements 
where Family_ID = 1 
ORDER BY Day, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

这当然行不通,但我只是想展示我正在努力实现的目标.客户不一定每天都需要帮助,我只想显示他们预订的天数.

This of course doesn't work but I just wanted to show what I am trying to achieve. The client doesn't necessarily require help every day, I just want to show the days they are booked in.

按 DESC 和 ASC 排序对一周中的几天没有帮助,如果您有任何有关如何实现这一点的提示,我将不胜感激.

Sorting by DESC and ASC doesn't help with days of the week, I would appreciate any tips on how to achieve this.

谢谢.

推荐答案

嗯.. 太糟糕了,日子被逐字存储为星期一"、星期二"等?

Hmm.. that's nasty, the days are stored as verbatim 'Monday', 'Tuesday', etc?

无论如何,就这样做:

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

这篇关于是否可以在 SQL Server 2008 中选择特定的 ORDER BY?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆