2

如何根据下表结构在输出中显示状态代码?

表名:

要求

列:

RequestID,RequestMessage,RequestStatusID,RequestPriorityID

例如

假设 RequestStatusID 转换为:

1 = IN_PROGRESS, 2 = CANCELED, 3 = COMPLETED

只是为了记录,我不允许创建一个新表来保存请求状态。我必须以某种方式将它们包含在 select 语句中,并且请求状态代码是否在 select 语句中被硬编码并不重要。

我只是想知道是否有另一种方法可以在不使用额外表格的情况下实现这一目标。

4

4 回答 4

4

案例陈述:

SELECT RequestID,
       CASE RequestStatusID 
       WHEN 1 THEN 'IN_PROGRESS'
       WHEN 2 THEN 'CANCELED'
       WHEN 3 THEN 'COMPLETED'
       END,
       RequestDesc,
       RequestPriorityID
 FROM #Request

或者如果您使用 SQL Server 2012,则使用 CHOOSE() 函数:

SELECT RequestID,
       CHOOSE(RequestStatusID,'IN_PROGRESS','CANCELED','COMPLETED'),
       RequestDesc,
       RequestPriorityID
 FROM #Request
于 2013-10-15T07:55:14.133 回答
1

您可以使用CASE

SELECT  *,
        CASE WHEN RequestStatusID = 1 THEN 'IN_PROGRESS'
             WHEN RequestStatusID = 2 THEN 'CANCELED'
             ELSE 'COMPLETED' 
        END StatusName
FROM    tableName
于 2013-10-15T07:54:57.323 回答
0

如果允许您创建一个函数,则可以使用 a ,Table Valued Function因为您不仅可以在此处使用它,还可以在其他查​​询中使用它。基本上易于维护和一致的结果。

表值函数:

create function dbo.RequestStatus()
returns @ReqStatus table
(
  id int identity(1,1), 
  status varchar(50)
)
as
begin

   insert into @ReqStatus values
   ('IN_PROGRESS'), ('CANCELED'), ('COMPLETED')

   return
end

GO

函数使用和查询(小提琴演示):

select r.RequestID,r.RequestMessage,r.RequestStatusID,
       r.RequestPriorityID, s.status
from Request r join (select id,status from dbo.RequestStatus()) s
    on r.RequestStatusID = s.id

结果:

| REQUESTID | REQUESTMESSAGE | REQUESTSTATUSID | REQUESTPRIORITYID |      STATUS |
|-----------|----------------|-----------------|-------------------|-------------|
|         1 |           Abcd |               1 |                 1 | IN_PROGRESS |
|         2 |           lmno |               2 |                 1 |    CANCELED |
|         3 |           pqrs |               3 |                 1 |   COMPLETED |
于 2013-10-15T08:15:35.857 回答
0

您可以使用case,但实际上我喜欢派生表的想法,因为将来更容易更改:

select
    R.*, C.Name as RequestStatusName
from Request as R
    left outer join (values
         (1, 'IN_PROGRESS'),
         (2, 'CANCELED'),
         (3, 'COMPLETED')
    ) as C(Id, Name) on C.Id = R.RequestStatusID 

因此,您可以添加任意数量的列,例如:

select
    R.*,
    C.Name as RequestStatusName,
    C.Description as RequestStatusDescription
from Request as R
    left outer join (values
         (1, 'IN_PROGRESS', 'In progress description'),
         (2, 'CANCELED', 'Canceled description'),
         (3, 'COMPLETED', 'Completed description')
    ) as C(Id, Name, Description) on C.Id = R.RequestStatusID 

如果您必须经常加入,您可以创建视图:

create view vw_RequestStatusNames
as
    select C.Id, C.Name, C.Description
    from (values
         (1, 'IN_PROGRESS', 'In progress description'),
         (2, 'CANCELED', 'Canceled description'),
         (3, 'COMPLETED', 'Completed description')
    ) as C(Id, Name, Description)

并在连接中使用它:

select
    R.*,
    C.Name as RequestStatusName,
    C.Description as RequestStatusDescription
from Request as R
    left outer join vw_RequestStatusNames as C on C.Id = R.RequestStatusID 

=> SQL FIDDLE 演示

于 2013-10-15T07:55:53.213 回答