1

我有一个需要从中提取信息的表。

SELECT [workID], [name], [status], [nextStep] FROM [JM_AccountWorkFlowDetail]

[nextStep]指向链中的下一步,如果可用,它将匹配workID.

上面的查询将返回:

workID  name                        status  nextStep
7       Name                        Status     0
9       Garnishment to Court        WWW        7

正如你所看到的workID9 它指向 7 这意味着workID7 是列表中的下一个。

是否可以显示名称nextStep而不是数字?

将返回以下内容的查询:

workID  name                        status  nextStep
7       Name                        Status     0
9       Garnishment to Court        WWW        Name
4

3 回答 3

4

将它与表本身连接起来,这样您就可以nextStep通过将其与workid.

SELECT  a.workID,
        a.name,
        a.status,
        COALESCE(b.name, CAST(a.nextStep AS VARCHAR(5))) nextStepName
FROM    JM_AccountWorkFlowDetail a
        LEFT JOIN JM_AccountWorkFlowDetail b
            ON a.nextStep = b.workID  
于 2013-07-25T20:00:30.940 回答
1
   ; with
    cte1 as
    (
        SELECT 

        JM.[workID]
        ,JM.[name]
        ,JM.[status]
        ,(select top 1 name from JM_AccountWorkFlowDetail where nextStep = JM.nextStep) nextStep
        FROM [JM_AccountWorkFlowDetail] JM
    )

    select workid, name, status, case when nextstep=0 then 'no next step' else nextstep end
    from cte1
于 2013-07-25T20:01:41.043 回答
0
SELECT a.[workID], a.[name], a.[status], b.name as nextstep FROM [JM_AccountWorkFlowDetail] a
right outer join [JM_AccountWorkFlowDetail] b
on b.nextstep=a.nextstep
于 2013-07-25T20:04:13.007 回答