数学涉及创建的列 [英] Math Involving Created Columns

查看:69
本文介绍了数学涉及创建的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据我在Microsoft SQL Server Management Studio 18中创建的列进行数学运算。我正在尝试对公司数据库中的列进行数学运算以转换​​值,但我正在尝试在将
移到下一步之前,要了解如何对已创建的列进行数学运算。

I'm trying to do math based on a column I've created in Microsoft SQL Server Management Studio 18. I'm trying to do math on columns within my companies database to convert values but I'm trying to understand how to do math on created columns before moving onto the next step.

以下是我正在尝试解决的示例

Here is an example I'm trying to work through

第1行               选择 

line 1                select 

第2行                1 + 2为第1列,

line 2                1+2 as column1,

第3行                1 + 2为第2列, 

line 3                1+2 as column2, 

第4行                  column1 + column2 as column3

line 4                 column1 + column2 as column3

第5行                  from DB

line 5                 from DB

输入后,我在第4行收到错误,因为column1和column2是无效的列名。有没有办法在这些新创建的列上做数学?

When this is entered I get an error on line 4 because column1 and column2 are invalid column names. Is there any way to do math on these newly created columns?

推荐答案

简短的回答是你不能。你必须重复这个表达。您的查询变为: 

The short answer is you can't. You must repeat the expression. Your query becomes: 

select 1 + 2 as column1, 
1 + 2 as column2, 
(1 + 2) + (1 + 2) as column3 
from dbo.table 
where ...
order by ...;


使用一个明显无意义的例子似乎相当愚蠢但是那些是规则 - 我添加了括号和一些额外的最佳实践以使事情更清晰。您可以使用CTE使这更具可读性,但更简单的可能是
更安全。您还可以创建一个视图(而不是使用CTE)。 



It seems rather silly using an obviously non-sense example but those are the rules - and I added parentheses and some additional best practices to make things more clear. You could make this somewhat more readable using a CTE but simpler is probably safer for the moment. You could also create a view (instead of using a CTE). 



这篇关于数学涉及创建的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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