TSQL - 是否可以定义排序顺序?
是否可以为返回的结果定义排序顺序?
我希望排序顺序为“橙色”“苹果”“草莓”,而不是升序或降序。
我知道 ORDER BY 可以执行 ASC 或 DESC 但是否有 DEFINED('orange', 'apple', 'strawberry') 类型的东西?
这将在 SQL Server 2000 上运行。
Is it possible to define a sort order for the returned results?
I would like the sort order to be 'orange' 'apple' 'strawberry' not ascending or descending.
I know ORDER BY can do ASC or DESC but is there a DEFINED('orange', 'apple', 'strawberry') type thing?
This will be running on SQL Server 2000.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它非常笨重,但您可以使用 CASE 语句进行排序:
或者,您可以创建一个包含排序字段和排序顺序的辅助表。
并将您的桌子加入到这张新桌子上。
It's incredibly clunky, but you can use a CASE statement for ordering:
Alternately, you can create a secondary table which contains the sort field and a sort order.
And join your table onto this new table.
使用 CASE 语句:
备用语法,带有 ELSE:
Use a CASE statement:
Alternate syntax, with an ELSE:
如果这是一个短暂的需求,请使用 case 语句。但是,如果您认为它可能会存在一段时间,并且始终会是橙色/苹果/草莓顺序(或者即使不是 - 见下文),您可能需要考虑牺牲一些磁盘空间以获得一些速度。
在表中创建一个名为
or_ap_st
的新列,并使用插入/更新触发器在其中填充数字 1、2 或 3,具体取决于水果列的值。然后对其进行索引。由于该列中的数据唯一发生更改的时间是行更改时,因此这是执行此操作的最佳时间。然后,少量写入而不是大量读取会产生成本,因此可以通过
select
语句进行摊销。然后,您的查询将快得令人眼花缭乱:
没有每行函数会降低性能。
而且,如果您还想要其他排序顺序,那么这就是我将该列称为
or_ap_st
的原因。您可以根据需要添加任意数量的其他排序列。If this is going to be a short-lived requirement, use a case statement. However, if you think it may be around for a while, and it's always going to be
orange/apple/strawberry
order (or even if not - see below), you may want to think about sacrificing some disk space to gain some speed.Create a new column in your table called
or_ap_st
and use an insert/update trigger to populate it with the number 1, 2 or 3, depending on the the value of your fruit column. Then index on it.Since the only time the data in that column will change is when the row changes, that's the best time to do it. The cost will then be incurred on a small number of writes rather than a large number of reads, hence amortised over the
select
statements.Your query will then be a blindingly fast:
with no per-row functions killing the performance.
And, if you want other sort orders as well, well, that's why I called the column
or_ap_st
. You can add as many other sorting columns as you need.在这种情况下我要做的是
What I do in that case is
从 turtlepick 的答案进一步看:
如果 FRUIT 中有更多项目,并且它们恰好以 THEN 关键字之后定义的字母开头,这些项目将出现在硬编码订单中。例如,香蕉出现在草莓之前。您可以使用以下方法规避它:
这里我使用了具有较低 ASCII 值的字符,希望它们不会出现在 FRUIT 中值的开头。
Going further from turtlepick's answer:
In case you have some more items in FRUIT and they happen to start with letters defined after THEN keywords, those items would appear within the hardcoded order. For example Banana shows up before Strawberry. You can circumvent it with
Here I have used characters with lower ASCII values in hope that they would not appear at the beginning of values in FRUIT.
在表中添加一个键(例如fruit_id int Identity(1,1)主键)以保留插入
结果的顺序:
Add a key to the table (e.g. fruit_id int identity(1,1) primary key) to preserve the order of insert
result: