[英]How do I alias particular values in SQL?
I will present a question about 'aliasing' values from a column. 我将在列中提出有关“别名”值的问题。 I will use days of the week as an intuitive example to get my question across, but I am not asking for datetime conversions.
我将以一周中的几天作为一个直观的例子来阐明我的问题,但我不是在要求日期时间转换。
Suppose I have the following SQL script: 假设我有以下SQL脚本:
SELECT DaysOfWeek
FROM [databasename].[dbo].[tablename]
Now, the column DaysOfWeek
will return string values of the days' names, ie "Monday," "Tuesday," and so forth. 现在,
DaysOfWeek
列将返回日期名称的字符串值,即“ Monday”,“ Tuesday”等。
What if I wanted the query to return the integer 1 for 'Monday', 2 for 'Tuesday', and so forth? 如果我希望查询为“星期一”返回整数1,为“星期二”返回整数2,以此类推呢? I would want to assign a particular value to each of the week's days in the
SELECT
statement, but I'm not sure how to go about doing that. 我想在
SELECT
语句中为一周中的每一天分配一个特定的值,但是我不确定该怎么做。
I'm relatively new to SQL, so I just thought I'd ask for an intuitive method to perform such a task. 我对SQL比较陌生,所以我只是想要求一种直观的方法来执行此类任务。
Edited to add: I'm only using days of the week and their respective integer representation as an easy example; 编辑添加:我仅使用星期几及其各自的整数表示作为简单示例; my task does not involve days of the week, but rather employee code numbers and corresponding titles.
我的任务不涉及星期几,而是涉及员工编号和相应的职务。
You can do this using case
: 您可以使用
case
进行此操作:
SELECT (CASE DaysOfWeek
WHEN 'Monday' THEN 1
WHEN 'Tuesday' THEN 2
. . .
END)
Under most circumstances, it is unnecessary to store the day of the week like this. 在大多数情况下,不必像这样存储星期几。 You can readily use a function,
datepart()
or datename()
, to extract the day of the week from a date/time value. 您可以方便地使用函数
datepart()
或datename()
从日期/时间值中提取星期几。
If the column is in a table, and not part of a date, then you might want to include the above logic as a computed column: 如果该列在表中,而不是日期的一部分,那么您可能希望将上述逻辑作为计算列包括在内:
alter table t add DayOfWeekNumber as (case DaysOfWeek when 'Monday' then 1 . . .);
If you wanted to define your own corresponding value for another value, the best way is to use a table, and join that table. 如果要为另一个值定义自己的对应值,最好的方法是使用一个表并将其联接。
For example: 例如:
create table dbo.EmployeeTitle (
id int not null identity(1,1) primary key
, title varchar(32)
);
create table dbo.Employee (
id int not null identity(1,1) primary key
, name nvarchar(128)
, title_id int references dbo.EmployeeTitle(id)
);
insert into dbo.EmployeeTitle values ('Big boss');
insert into dbo.Employee values ('daOnlyBG',1);
select e.*, et.title
from dbo.Employee e
inner join dbo.EmployeeTitle et
on e.title_id = et.id
rextester demo: http://rextester.com/FXIM78632 extrester演示: http ://rextester.com/FXIM78632
returns: 返回:
+----+----------+----------+----------+
| id | name | title_id | title |
+----+----------+----------+----------+
| 1 | daOnlyBG | 1 | Big boss |
+----+----------+----------+----------+
The easiest way I can think of is to have a table variable or CTE; 我想到的最简单的方法是拥有一个表变量或CTE。 create your lookup as rows and join to it.
将您的查找创建为行并将其加入。 Something like this:
像这样:
with cte as (
select 1 as emp_code, 'value1' as emp_title
union
select 2 as emp_code, 'value2' as emp_title
union
select 3 as emp_code, 'value3' as emp_title
)
select cte.emp_code, tableName.*
from tableName
inner join cte
on cte.emp_title = tableName.some_column
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.