0

我发现“CASE WHEN”语句对我的查询非常有用。但这是我的查询(仅选择):

SELECT  dbo.ARCHIVE.SYSTEM_KEY AS PROTOCOLLO, 
        CASE dbo.ARCHIVEDEST.ERR_ID WHEN 0 THEN 'OK' ELSE 'KO' END AS ESITO, 
        CASE WHEN dbo.ARCHIVEDEST.XMODE IN ('R', 'K', 'H') THEN 'RX' ELSE 'TX' END AS 'T/R', 
        CASE 'T/R' WHEN 'TX' THEN CONTACTORIGIN.address ELSE CONTACTDESTINATION.address END AS Utente, 

问题出在第三个 Case 语句上,因为不评估以前的“T/R”查询(对于所有记录,它返回“CONTACTORIGIN.address”。有可能做到这一点?或者我走错路了?

4

4 回答 4

0

您必须重复测试:

SELECT  dbo.ARCHIVE.SYSTEM_KEY AS PROTOCOLLO, 
        CASE dbo.ARCHIVEDEST.ERR_ID WHEN 0 THEN 'OK' ELSE 'KO' END AS ESITO, 
        CASE WHEN dbo.ARCHIVEDEST.XMODE IN ('R', 'K', 'H') THEN 'RX' ELSE 'TX' END AS 'T/R', 
        CASE CASE WHEN dbo.ARCHIVEDEST.XMODE NOT IN ('R', 'K', 'H') THEN CONTACTORIGIN.address ELSE CONTACTDESTINATION.address END AS Utente, 

或者你可以做一个子查询:

SELECT PROTOCOLLO, ESITO, [T/R],
    CASE WHEN [T/R] = 'TX' THEN CONTACTORIGIN.address ELSE CONTACTDESTINATION.address END AS Utente
FROM
(SELECT  dbo.ARCHIVE.SYSTEM_KEY AS PROTOCOLLO, 
        CASE dbo.ARCHIVEDEST.ERR_ID WHEN 0 THEN 'OK' ELSE 'KO' END AS ESITO, 
        CASE WHEN dbo.ARCHIVEDEST.XMODE IN ('R', 'K', 'H') THEN 'RX' ELSE 'TX' END AS [T/R], 
) s
于 2013-06-11T08:17:30.513 回答
0

使用子查询来定义T/R别名;

SELECT  *
,       CASE 'T/R' WHEN 'TX' THEN CONTACTORIGIN.address ELSE CONTACTDESTINATION.address 
            END AS Utente
FROM    (
        SELECT  CASE dbo.ARCHIVEDEST.ERR_ID WHEN 0 THEN 'OK' ELSE 'KO' END AS ESITO
        ,       CASE WHEN dbo.ARCHIVEDEST.XMODE IN ('R', 'K', 'H') THEN 'RX' ELSE 'TX' 
                    END AS 'T/R'
        FROM    dbo.ARCHIVE.SYSTEM_KEY AS PROTOCOLLO
        ) as SubQueryAlias
于 2013-06-11T08:20:11.160 回答
0

当您为SELECT子句中的列计算值时,不允许访问在同一SELECT子句中计算的任何其他计算值 - 因为(根据 SQL 标准)列是并行计算的(它们可能不是,但假设某些实现会这样做),因此计算值尚不可用。

您可以将“早期”计算放在子查询或 CTE 中:

SELECT *,
    CASE [T/R] WHEN 'TX' THEN CONTACTORIGIN.address ELSE CONTACTDESTINATION.address END AS Utente
FROM (
    SELECT  dbo.ARCHIVE.SYSTEM_KEY AS PROTOCOLLO, 
        CASE dbo.ARCHIVEDEST.ERR_ID WHEN 0 THEN 'OK' ELSE 'KO' END AS ESITO, 
        CASE WHEN dbo.ARCHIVEDEST.XMODE IN ('R', 'K', 'H') THEN 'RX' ELSE 'TX' END AS [T/R], 

    ...
) t
于 2013-06-11T08:22:01.623 回答
0

如果您不想重复您的案例陈述,您有两个选择,第一个是将其中一个移动到子查询,即

SELECT  PROTOCOLLO,
        ESITO,
        [T/R],
        CASE [T/R] WHEN 'TX' THEN CONTACTORIGIN.address ELSE CONTACTDESTINATION.address END AS Utente
FROM    (   SELECT  dbo.ARCHIVE.SYSTEM_KEY AS PROTOCOLLO, 
                    CASE dbo.ARCHIVEDEST.ERR_ID WHEN 0 THEN 'OK' ELSE 'KO' END AS ESITO, 
                    CASE WHEN dbo.ARCHIVEDEST.XMODE IN ('R', 'K', 'H') THEN 'RX' ELSE 'TX' END AS 'T/R'
            FROM    ....
        ) SubQuery

或者您可以将您的 CASE 语句移动到CROSS APPLY

SELECT  dbo.ARCHIVE.SYSTEM_KEY AS PROTOCOLLO, 
        CASE dbo.ARCHIVEDEST.ERR_ID WHEN 0 THEN 'OK' ELSE 'KO' END AS ESITO, 
        'T/R', 
        CASE [T/R] WHEN 'TX' THEN CONTACTORIGIN.address ELSE CONTACTDESTINATION.address END AS Utente
FROM    ...
        CROSS APPLY
        (   SELECT  CASE WHEN dbo.ARCHIVEDEST.XMODE IN ('R', 'K', 'H') THEN 'RX' ELSE 'TX' END AS 'T/R'
        ) tx
于 2013-06-11T08:20:00.220 回答