简体   繁体   English

如何在SQL中的计算字段上使用order by?

[英]How to use order by on a calculated field in SQL?

How to use order by on a calculated field in SQL? 如何在SQL中的计算字段上使用order by?

select a.Customer
    ,a.PlanTo
    ,a.Dollar01
    ,a.Dollar02
    ,a.Dollar03
    ,a.Dollar04
    ,a.Dollar05
    ,a.Dollar06
    ,a.Dollar07
    ,a.Dollar08
    ,a.Dollar09
    ,a.Dollar10
    ,a.Dollar11
    ,a.Dollar12
    ,(CAST(a.Dollar01 as decimal) + CAST(a.Dollar02 as decimal) 
    + CAST(a.Dollar03 as decimal) + CAST(a.Dollar04 as decimal) 
    + CAST(a.Dollar05 as decimal) + CAST(a.Dollar06 as decimal) 
    + CAST(a.Dollar07 as decimal) + CAST(a.Dollar08 as decimal) 
    + CAST(a.Dollar09 as decimal) + CAST(a.Dollar10 as decimal) 
    + CAST(a.Dollar11 as decimal) + CAST(a.Dollar12 as decimal)) as TOTAL1
from MDM_STAT.sds.SMarginText a
where a.salesyear = '2016'
order by a.total1

This is giving me 'Total1' column does not exist but as you see I have created it and is working if I am not using the order by clause. 这给了我“ Total1”列不存在,但是如您所见,我已经创建了它,并且如果我不使用order by子句,它也可以正常工作。

You can do what Mureinik suggests and use the ordinal notation of ORDER BY 13 , which means "order by the 13th column." 您可以按照Mureinik的建议进行操作,并使用ORDER BY 13的序号表示,即“第13列的顺序”。 However, I would tend to avoid it because it's difficult to tell what you're intending to order by if you come back later. 但是,我倾向于避免这种情况,因为如果以后再回来很难说出您打算订购什么。 Also, if you need to add a column or change the order, you have to remember to update the ORDER BY clause. 同样,如果您需要添加一列或更改顺序,则必须记住要更新ORDER BY子句。 It's easy to miss that. 很容易错过这一点。

As others mention in the comments, it is possible to use the alias you specify in the ORDER BY. 就像其他人在评论中提到的那样,可以使用您在ORDER BY中指定的别名。 However, because it's a column alias, there's nothing to fully qualify. 但是,因为它是列别名,所以没有完全限定的条件。 a.TOTAL1 doesn't mean anything. a.TOTAL1并不代表什么。 You must ORDER BY TOTAL1 : 您必须按ORDER BY TOTAL1

select a.Customer
    ,a.PlanTo
    ,a.Dollar01
    ,a.Dollar02
    ,a.Dollar03
    ,a.Dollar04
    ,a.Dollar05
    ,a.Dollar06
    ,a.Dollar07
    ,a.Dollar08
    ,a.Dollar09
    ,a.Dollar10
    ,a.Dollar11
    ,a.Dollar12
    ,(CAST(a.Dollar01 as decimal) + CAST(a.Dollar02 as decimal) 
    + CAST(a.Dollar03 as decimal) + CAST(a.Dollar04 as decimal) 
    + CAST(a.Dollar05 as decimal) + CAST(a.Dollar06 as decimal) 
    + CAST(a.Dollar07 as decimal) + CAST(a.Dollar08 as decimal) 
    + CAST(a.Dollar09 as decimal) + CAST(a.Dollar10 as decimal) 
    + CAST(a.Dollar11 as decimal) + CAST(a.Dollar12 as decimal)) as TOTAL1
from MDM_STAT.sds.SMarginText a
where a.salesyear = '2016'
order by total1

This works because of the query solve order. 之所以有效,是因为查询解决顺序。 ORDER BY is solved after the SELECT, unlike WHERE or FROM which are solved before the SELECT and therefore can't refer to column aliases in SQL Server. ORDER BY是在SELECT之后解决的,而WHERE或FROM是在SELECT之前解决的,因此不能在SQL Server中引用列别名。

This can be confusing or ambiguous if your column alias has the same name as a column from a table, so you need to be aware of that. 如果您的列别名与表中的列具有相同的名称,这可能会造成混淆或模棱两可,因此您需要意识到这一点。

Posting Aaron Bertrand's comment as a community wiki, as I think it's the most straight forward answer: 将Aaron Bertrand的评论发布为社区Wiki,因为我认为这是最直接的答案:

a.total1 doesn't exist, since SQL Server will look for that column in SMarginText , but ORDER BY TOTAL1; a.total1不存在,因为SQL Server将在SMarginText查找该列,但ORDER BY TOTAL1; would work just fine. 会很好。

最简单的方法是使用选择列表中的列号代替其名称: ORDER BY 13

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM