案例语句查询

Case statement query

我可以在一个字段上使用 case 语句来更改另一个字段的值吗?诸如此类,

SELECT  TaskDescription, 
CASE 
When TaskDescription='Lab Dips / Handloom Sent' then seq ='1'  
When TaskDescription='Lab Dips / Handloom Approve' then seq ='2' end,seq FROM SomeTable

我在 TaskDescription 列中有一些任务描述,我想按特定顺序获取。这就是为什么我想为每个任务描述值添加一个序列号,这样我就可以获得预期的顺序,只需编写 'order by seq'

如果你想更新 table 的值,你可以使用这个:

Update SomeTable
Set seq = '1'
Where TaskDescription='Lab Dips / Handloom Sent'

更新:

Select TaskDescription ,seq 
From (
      SELECT  TaskDescription, 
            (CASE 
             When TaskDescription='Lab Dips / Handloom Sent' then 1 
             When TaskDescription='Lab Dips / Handloom Approve' then 2
           end) As seq FROM SomeTable
     ) As TB 
Order by TB.seq

或:

With tb As (
    SELECT  TaskDescription
           ,(CASE TaskDescription
             When 'Lab Dips / Handloom Sent' then 1 
             When 'Lab Dips / Handloom Approve' then 2
          end) As seq 
     FROM SomeTable
)
Select * from tb
Order by tb.seq