TSQL - 是否可以定义排序顺序?

发布于 2024-10-14 10:01:36 字数 186 浏览 3 评论 0原文

是否可以为返回的结果定义排序顺序?

我希望排序顺序为“橙色”“苹果”“草莓”,而不是升序或降序。

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱已欠费 2024-10-21 10:01:36

它非常笨重,但您可以使用 CASE 语句进行排序:

SELECT * FROM Blah 
ORDER BY CASE MyColumn 
    WHEN 'orange' THEN 1 
    WHEN 'apple' THEN 2 
    WHEN 'strawberry' THEN 3 
    END 

或者,您可以创建一个包含排序字段和排序顺序的辅助表。

TargetValue  SortOrder
orange       1
apple        2
strawberry   3

并将您的桌子加入到这张新桌子上。

It's incredibly clunky, but you can use a CASE statement for ordering:

SELECT * FROM Blah 
ORDER BY CASE MyColumn 
    WHEN 'orange' THEN 1 
    WHEN 'apple' THEN 2 
    WHEN 'strawberry' THEN 3 
    END 

Alternately, you can create a secondary table which contains the sort field and a sort order.

TargetValue  SortOrder
orange       1
apple        2
strawberry   3

And join your table onto this new table.

猥︴琐丶欲为 2024-10-21 10:01:36

使用 CASE 语句

ORDER BY CASE your_col
           WHEN 'orange' THEN 1
           WHEN 'apple' THEN 2
           WHEN 'strawberry' THEN 3
         END 

备用语法,带有 ELSE:

ORDER BY CASE 
           WHEN your_col = 'orange' THEN 1
           WHEN your_col = 'apple' THEN 2
           WHEN your_col = 'strawberry' THEN 3
           ELSE 4
         END 

Use a CASE statement:

ORDER BY CASE your_col
           WHEN 'orange' THEN 1
           WHEN 'apple' THEN 2
           WHEN 'strawberry' THEN 3
         END 

Alternate syntax, with an ELSE:

ORDER BY CASE 
           WHEN your_col = 'orange' THEN 1
           WHEN your_col = 'apple' THEN 2
           WHEN your_col = 'strawberry' THEN 3
           ELSE 4
         END 
GRAY°灰色天空 2024-10-21 10:01:36

如果这是一个短暂的需求,请使用 case 语句。但是,如果您认为它可能会存在一段时间,并且始终会是橙色/苹果/草莓顺序(或者即使不是 - 见下文),您可能需要考虑牺牲一些磁盘空间以获得一些速度。

在表中创建一个名为 or_ap_st 的新列,并使用插入/更新触发器在其中填充数字 1、2 或 3,具体取决于水果列的值。然后对其进行索引。

由于该列中的数据唯一发生更改的时间是行更改时,因此这是执行此操作的最佳时间。然后,少量写入而不是大量读取会产生成本,因此可以通过 select 语句进行摊销。

然后,您的查询将快得令人眼花缭乱:

select field1, field2 from table1
order by or_ap_st;

没有每行函数会降低性能。

而且,如果您还想要其他排序顺序,那么这就是我将该列称为 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:

select field1, field2 from table1
order by or_ap_st;

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.

兮子 2024-10-21 10:01:36

在这种情况下我要做的是

ORDER BY
  CASE WHEN FRUIT = 'Orange' THEN 'A' 
       WHEN FRUIT = 'Apple' THEN 'B'
       WHEN FRUIT = 'Strawberry' THEN 'C'
       ELSE FRUIT
END

What I do in that case is

ORDER BY
  CASE WHEN FRUIT = 'Orange' THEN 'A' 
       WHEN FRUIT = 'Apple' THEN 'B'
       WHEN FRUIT = 'Strawberry' THEN 'C'
       ELSE FRUIT
END
酒浓于脸红 2024-10-21 10:01:36

turtlepick 的答案进一步看:

ORDER BY
  CASE WHEN FRUIT = 'Orange' THEN 'A' 
       WHEN FRUIT = 'Apple' THEN 'B'
       WHEN FRUIT = 'Strawberry' THEN 'C'
       ELSE FRUIT
  END

如果 FRUIT 中有更多项目,并且它们恰好以 THEN 关键字之后定义的字母开头,这些项目将出现在硬编码订单中。例如,香蕉出现在草莓之前。您可以使用以下方法规避它:

ORDER BY
  CASE
    WHEN FRUIT = 'Orange' THEN '.1'
    WHEN FRUIT = 'Apple' THEN '.2'
    WHEN FRUIT = 'Strawberry' THEN '.3'
    ELSE FRUIT
  END

这里我使用了具有较低 ASCII 值的字符,希望它们不会出现在 FRUIT 中值的开头。

Going further from turtlepick's answer:

ORDER BY
  CASE WHEN FRUIT = 'Orange' THEN 'A' 
       WHEN FRUIT = 'Apple' THEN 'B'
       WHEN FRUIT = 'Strawberry' THEN 'C'
       ELSE FRUIT
  END

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

ORDER BY
  CASE
    WHEN FRUIT = 'Orange' THEN '.1'
    WHEN FRUIT = 'Apple' THEN '.2'
    WHEN FRUIT = 'Strawberry' THEN '.3'
    ELSE FRUIT
  END

Here I have used characters with lower ASCII values in hope that they would not appear at the beginning of values in FRUIT.

二智少女 2024-10-21 10:01:36

在表中添加一个键(例如fruit_id int Identity(1,1)主键)以保留插入

create table fruit(fruit_id int identity(1,1) primary key, name varchar(50))
go

insert into fruit(name) values ('orange')
insert into fruit(name) values ('apple')
insert into fruit(name) values ('strawberry')

select name from fruit

结果的顺序:

orange
apple
strawberry

Add a key to the table (e.g. fruit_id int identity(1,1) primary key) to preserve the order of insert

create table fruit(fruit_id int identity(1,1) primary key, name varchar(50))
go

insert into fruit(name) values ('orange')
insert into fruit(name) values ('apple')
insert into fruit(name) values ('strawberry')

select name from fruit

result:

orange
apple
strawberry
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文