更改SQL查询中的返回值

时间:2017-03-31 07:53:11

标签: mysql sql sql-server

我正在使用实时查看程序来进行SQL查询(并让我编辑代码的一部分)

目前代码是:

SELECT 
    [LogonName] AS [Logon name], 
    [LogonExtension] AS [Logon extension], 
    [VoiceReady] AS [Ready for Voice], 
    [CallState] AS [Call state]
FROM 
    [AgentPerformance]
WHERE 
    (AgentID IN (1, 2, 3))

返回:

enter image description here

现在我的问题是,有没有办法改变返回的“呼叫状态”值,让我们说忙,免费,处理等...(有3个不同的值)。

我试过例如:

SELECT 
    [LogonName] AS [Logon name], 
    [LogonExtension] AS [Logon extension], 
    [VoiceReady] AS [Ready for Voice], 
    [CallState] AS [Call state]
FROM
    [AgentPerformance]
WHERE 
    (AgentID IN (1, 2, 3))

SELECT 
    CASE 
       WHEN CallState = 0 THEN 'No'
       WHEN CallState = 1 THEN 'Yes
       ELSE 'Maybe'
    END AS kakaduu
FROM 
    AgentPerformance

但这对我不起作用。

1 个答案:

答案 0 :(得分:3)

您的CASE表达式必须与其他列一起位于SELECT列表中。第二个SELECT在您的示例中是不正确的语法。

所以,试试这个:

SELECT [LogonName] AS [Logon name], [LogonExtension] AS [Logon extension], [VoiceReady] AS [Ready for Voice], [CallState] AS [Call state],
CASE 
    WHEN CallState = 0 then 'No'
    WHEN CallState = 1 then 'Yes'
    ELSE 'Maybe'
END
AS kakaduu
FROM [AgentPerformance]
WHERE   AgentID IN (1, 2, 3)